NSImage DPI Question

Can someone tell me how can I get the true width and height of NSImage? I noticed that images with a higher DPI than 72 have inaccurate width and height with NSImage.size options.

I saw this example on Cocoadev:

NSBitmapImageRep *rep = [image bestRepresentationForDevice: nil];

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

However, bestRepresentationForDevice is deprecated by 10.6 ... what can I use as an alternative, the documentation does not offer another method?

+3
source share
3 answers

Create NSBitmapImageRep from TIFF NSImage View

NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
+4
source

, , . Calling -bestRepresentationForRect: context: : , .

+1
@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

Source: https://habr.com/ru/post/1786698/


All Articles