3

谁能告诉我如何获得 NSImage 的真实宽度和高度?我注意到 DPI 高于 72 的图像通过 NSImage.size 参数的宽度和高度不准确。

我在 Cocoadev 上看到了这个例子:

NSBitmapImageRep *rep = [image bestRepresentationForDevice: nil];

NSSize pixelSize = NSMakeSize([rep pixelsWide],[rep pixelsHigh]);

但是 bestRepresentationForDevice 在 10.6 上已弃用...我可以使用什么作为替代方法,文档没有提供不同的方法?

4

3 回答 3

4

从 NSImage 的 TIFF 表示构建您的 NSBitmapImageRep

NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
于 2011-01-20T16:58:25.880 回答
1

遍历图像的表示以寻找具有最大 -size 的表示。如果您提供一个非常大的矩形,调用 -bestRepresentationForRect:context:hints: 应该为您执行此操作。

于 2012-05-16T20:27:19.197 回答
0
@interface NSImage (Representation)
-(NSBitmapImageRep*)bitmapRepresentation;
@end

@implementation NSImage (Representation)
-(NSBitmapImageRep*)bitmapRepresentation {
    for (id thing in self.representations) {
        if (![thing isKindOfClass:[NSBitmapImageRep class]]) continue;
        return (NSBitmapImageRep*)thing;
    }
    return nil;
}
@end
于 2012-05-15T12:36:27.823 回答