1

次のように作成された、PDFデータで初期化されたNSImageがあります。

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

4

2 に答える 2