【问题标题】:NSImage DPI QuestionNSImage DPI 问题
【发布时间】:2012-05-17 04:27:19
【问题描述】:
谁能告诉我如何获得 NSImage 的真实宽度和高度?我注意到 DPI 高于 72 的图像会通过 NSImage.size 参数显示不准确的宽度和高度。
我在 Cocoadev 上看到了这个例子:
NSBitmapImageRep *rep = [image bestRepresentationForDevice: nil];
NSSize pixelSize = NSMakeSize([rep pixelsWide],[rep pixelsHigh]);
但是 bestRepresentationForDevice 在 10.6 上已弃用...我可以使用什么作为替代方法,文档没有提供不同的方法?
【问题讨论】:
标签:
objective-c
cocoa
dpi
nsimage
【解决方案1】:
从 NSImage 的 TIFF 表示构建您的 NSBitmapImageRep
NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
【解决方案2】:
遍历图像的表示,寻找具有最大 -size 的表示。如果您提供一个非常大的矩形,调用 -bestRepresentationForRect:context:hints: 应该会为您执行此操作。
【解决方案3】:
@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