我有一个NSImage中,用PDF数据,像这样创建初始化:NSImage中和PDFImageRep缓存仍然吸引仅
NSData* data = [view dataWithPDFInsideRect:view.bounds];
slideImage = [[NSImage alloc] initWithData:data];
的slideImage
现在是view
的大小。
当我尝试在NSImageView
中渲染图像时,即使您清除缓存或更改图像大小,图像视图与图像的原始大小完全相同时,图像也只会变得尖锐。我试图设置cacheMode
到NSImageCacheNever
,这也没有工作。图像中唯一的图像代表PDF,当我将它渲染为PDF文件时,它显示它是矢量。
作为一种变通方法,我创建了一个NSBitmapImageRep
不同尺寸,调用drawInRect
原始图像上,并把位图表示新NSImage
内和渲染,其工作,但感觉像它不是最佳:
- (NSBitmapImageRep*)drawToBitmapOfWidth:(NSInteger)width
andHeight:(NSInteger)height
withScale:(CGFloat)scale
{
NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:width * scale
pixelsHigh:height * scale
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0
];
bmpImageRep = [bmpImageRep bitmapImageRepByRetaggingWithColorSpace:
[NSColorSpace sRGBColorSpace]];
[bmpImageRep setSize:NSMakeSize(width, height)];
NSGraphicsContext *bitmapContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:bmpImageRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bitmapContext];
[self drawInRect:NSMakeRect(0, 0, width, height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1];
[NSGraphicsContext restoreGraphicsState];
return bmpImageRep;
}
- (NSImage*)rasterizedImageForSize:(NSSize)size
{
NSImage* newImage = [[NSImage alloc] initWithSize:size];
NSBitmapImageRep* rep = [self drawToBitmapOfWidth:size.width andHeight:size.height withScale:1];
[newImage addRepresentation:rep];
return newImage;
}
如何在不诉诸像我这样的黑客的情况下以任何大小很好地呈现PDF?
你能否详细说明一下?我一直无法扩大我的图像质量 - 只有在绘制pdfpage到图像时的大小。非常感谢 – flooie