Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSImage acting weird

Why is this code setting artistImage to an image with 0 width and 0 height?

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

like image 422
Chetan Avatar asked Apr 09 '26 14:04

Chetan


2 Answers

As Ken wrote, the DPI is messed up in this image. If you want to force NSImage to set the real image size (ignoring the DPI), use the method described at http://borkware.com/quickies/one?topic=NSImage:

NSBitmapImageRep *rep = [[image representations] objectAtIndex: 0];
NSSize size = NSMakeSize([rep pixelsWide], [rep pixelsHigh]);
[image setSize: size];
like image 78
Kuba Suder Avatar answered Apr 12 '26 02:04

Kuba Suder


NSImage does load this fine for me, but that particular image has corrupt metadata. Its resolution according to the exif data is 7.1999997999228071e-06 dpi.

NSImage respects the DPI info in the file, so if you try to draw the image at its natural size, you'll get something 2520000070 pixels across.

like image 44
Ken Avatar answered Apr 12 '26 03:04

Ken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!