NSImage DPI问题

4

有人能告诉我如何获取NSImage的真实宽度和高度吗?我注意到,比72更高DPI的图像通过NSImage.size参数传递时,其宽度和高度不准确。

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

NSBitmapImageRep *rep = [image bestRepresentationForDevice: nil];

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

然而,bestRepresentationForDevice在10.6上已被弃用...我可以使用什么替代方法?文档没有提供其他的方法吗?

3个回答

5

从NSImage的TIFF表示中构建您的NSBitmapImageRep

NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];

1

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


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接