NSImage is acting weird

Why does this code set artistImage to an image with widths of 0 and 0?

NSURL * artistImageURL = [NSURL URLWithString: @ " http://userserve-ak.last.fm/serve/252/8581581.jpg "]; NSImage * artistImage = [[NSImage alloc] initWithContentsOfURL: artistImageURL];

+3
source share
4 answers

NSImage does download this for me, but this image has corrupted metadata. Its resolution according to exif is 7.1999997999228071e-06 dpi.

NSImage DPI , , , - 2520000070 .

+1

, DPI . NSImage ( DPI), , http://borkware.com/quickies/one?topic=NSImage:

NSBitmapImageRep *rep = [[image representations] objectAtIndex: 0];
NSSize size = NSMakeSize([rep pixelsWide], [rep pixelsHigh]);
[image setSize: size];
+4

, , NSImage's -initWithContentsOfURL: URL- . URL-, -initWithData:

0

, . NSImageRep * (, NSBitmapImageRep). , - . (, .icns .tiff).

@implementation NSImage (Extension)

- (void) makePixelSized {
    NSSize max = NSZeroSize;
    for (NSObject* o in self.representations) {
        if ([o isKindOfClass: NSImageRep.class]) {
            NSImageRep* r = (NSImageRep*)o;
            if (r.pixelsWide != NSImageRepMatchesDevice && r.pixelsHigh != NSImageRepMatchesDevice) {
                max.width = MAX(max.width, r.pixelsWide);
                max.height = MAX(max.height, r.pixelsHigh);
            }
        }
    }
    if (max.width > 0 && max.height > 0) {
        self.size = max;
    }
}

@end
0

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


All Articles