Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the correct image width and height of an NSImage

Tags:

I use the code below to get the width and height of a NSImage:

NSImage *image = [[[NSImage alloc] initWithContentsOfFile:[NSString stringWithFormat:s]] autorelease]; imageWidth=[image size].width; imageHeight=[image size].height; NSLog(@"%f:%f",imageWidth,imageHeight); 

But sometime imageWidth, imageHeight does not return the correct value. For example when I read an image, the EXIF info displays:

PixelXDimension = 2272; PixelYDimension = 1704; 

But imageWidth, imageHeight outputs

521:390  
like image 884
arachide Avatar asked Aug 09 '12 04:08

arachide


1 Answers

Dimensions of your image in pixels is stored in NSImageRep of your image. If your file contains only one image, it will be like this:

NSImageRep *rep = [[image representations] objectAtIndex:0]; NSSize imageSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh); 

where image is your NSImage and imageSize is your image size in pixels.

like image 77
Kibernetik Avatar answered Oct 20 '22 11:10

Kibernetik