开发者

NSImage DPI Question

开发者 https://www.devze.com 2023-02-05 13:18 出处:网络
Can anyone tell me how I can get the true width and height of an NSImage? I have noticed that images that are a higher DPI than 72 come through with inaccurate width and height with the NSImage.size p

Can anyone tell me how I can get the true width and height of an NSImage? I have noticed that images that are a higher DPI than 72 come through with inaccurate width and height with the NSImage.size parameters.

I saw this example on Cocoadev:

NSBitmapImageRep *rep = [image bestRepresentationForDevice: nil];

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

Howe开发者_开发知识库ver bestRepresentationForDevice is deprecated on 10.6... what can I use as an alternative, the documentation doesn't offer a different method?


Build your NSBitmapImageRep from the TIFF representation of the NSImage

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


Iterate through the image's representation looking for the one with the largest -size. Calling -bestRepresentationForRect:context:hints: should do this for you if you feed an extremely large rectangle.


@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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号