我有一个用PDF数据初始化的NSImage,创建如下:
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在任何大小下都能很好地呈现,而不是像我这样求助于黑客呢?
发布于 2013-10-30 19:10:11
NSImage的要点在于,您可以按照您希望的大小(以磅为单位)创建它。支持表示可以是基于矢量的(例如,PDF),并且NSImage是独立于分辨率的(即,它支持每个点不同的像素),但是NSImage仍然具有固定大小(以点为单位)。
NSImage的一个要点是它将/可以添加一个缓存表示来加速后续的绘制。
如果您需要绘制多种大小的PDF,并且想要使用NSImage,那么您最好为给定的目标大小创建一个NSImage。如果您愿意,您可以保留NSPDFImageRef --我认为它不会为您节省太多。
发布于 2013-10-30 21:53:17
我们尝试了以下方法:
NSPDFImageRep* rep = self.representations.lastObject;
return [NSImage imageWithSize:size flipped:NO drawingHandler:^BOOL (NSRect dstRect)
{
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[rep drawInRect:dstRect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1 respectFlipped:YES hints:@{
NSImageHintInterpolation: @(NSImageInterpolationHigh)
}];
return YES;
}];这确实会在放大时给你带来很好的效果,但在缩小时会产生模糊的图像。
https://stackoverflow.com/questions/19679656
复制相似问题