簡體   English   中英

NSImage和PDFImageRep緩存仍然只能以一種分辨率繪制

[英]NSImage and PDFImageRep caching still draws at only one resolution

我有一個用PDF數據初始化的NSImage,其創建方式如下:

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

slideImage現在是view的大小。

當我嘗試在NSImageView渲染圖像時,即使您清除了緩存或更改了圖像大小,它也僅在圖像視圖恰好是圖像的原始大小時才繪制清晰的圖像。 我嘗試將cacheMode設置為NSImageCacheNever ,這也沒有用。 圖像中唯一的圖像表示是PDF,當我將其渲染為PDF文件時,它表示它是矢量。

解決方法是,創建一個具有不同大小的NSBitmapImageRep ,在原始圖像上調用drawInRect ,然后將位圖表示形式放入新的NSImage然后進行渲染,這是NSBitmapImageRep ,但是感覺並非最佳:

- (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來以任意大小很好地呈現而不用像我這樣的黑客?

NSImage的要點是您使用所需的大小(以NSImage為單位)來創建它。 支持表示可以基於矢量(例如PDF),並且NSImage是與分辨率無關的(即,每個點支持不同的像素),但是NSImage仍然具有固定的大小(以點為單位)。

NSImage的要點之一是,它將/可以添加緩存表示以加快后續繪制的速度。

如果您需要將PDF繪制為多種尺寸,並且要使用NSImage,則最好為給定的目標尺寸創建一個NSImage。 如果願意,可以保留NSPDFImageRef左右-我認為不會為您節省很多。

我們嘗試了以下方法:

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

在放大時確實可以得到不錯的結果,但在縮小時卻會使圖像模糊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM