次のように作成された、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 を任意のサイズで適切にレンダリングするにはどうすればよいですか?