2013-10-30 17 views
1
一项决议

我有一个NSImage中,用PDF数据,像这样创建初始化:NSImage中和PDFImageRep缓存仍然吸引仅

NSData* data = [view dataWithPDFInsideRect:view.bounds]; 
slideImage = [[NSImage alloc] initWithData:data]; 

slideImage现在是view的大小。

当我尝试在NSImageView中渲染图像时,即使您清除缓存或更改图像大小,图像视图与图像的原始大小完全相同时,图像也只会变得尖锐。我试图设置cacheModeNSImageCacheNever,这也没有工作。图像中唯一的图像代表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?

回答

1

NSImage的要点是您可以使用您希望的尺寸(以点为单位)创建它。背景表示可以是基于矢量的(例如PDF),并且分辨率是独立的(即它支持每点不同的像素),但是NSImage仍然具有固定的大小(以点为单位)。

NSImage的一个要点是它可以添加一个缓存表示来加速后续的绘制。

如果您需要绘制PDF到多种尺寸,并且您想要使用NSImage,您可能最好为给定的目标尺寸创建NSImage。如果你愿意,你可以保留NSPDFImageRef - 我认为它不会为你节省很多。

0

我们尝试了以下内容:

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; 
}]; 

而任何放大的时候给你很好的结果,但缩小时,使对模糊的图像 。

+0

你能否详细说明一下?我一直无法扩大我的图像质量 - 只有在绘制pdfpage到图像时的大小。非常感谢 – flooie

相关问题