Re: NSImage size?
Re: NSImage size?
- Subject: Re: NSImage size?
- From: Heinrich Giesen <email@hidden>
- Date: Tue, 24 May 2005 10:21:31 +0200
An NSImage is a wrapper (container) of zero, one or more
imageRepresentations. You cannot ask an NSImage for its
pixelSize because it has no pixels, only its representations
may (or may not) have pixels.
A simple way to get the values you are interested in, is:
id theRep = [NSImageRep imageRepWithContentsOfFile:imagepath];
If your file contains JPEG data the resulting theRep is an
NSBitmapImageRep. Its size is:
NSSize size = [theRep size];
The number of pixels are:
int pixelsWide = [theRep pixelsWide];
int pixelsHigh = [theRep pixelsHigh];
Most jpeg files have no information about the resolution of
the image, 72 dpi is assumed. Some jepg files have an additional
App0 marker which may contain informations about the resolution.
In your example the resolution is 180 dpi which can be found with:
float dpiX = 72.0*[theRep pixelsWide]/[theRep size].width;
float dpiY = 72.0*[theRep pixelsHigh]/[theRep size].height;
To get these values you don't need an NSImage. But, if you really
want to have an NSImage, you can create one with:
NSImage *theImage = [[NSImage alloc] initWithSize:NSZeroSize];
[theImage addRepresentation:theRep];
On 24.05.2005, at 02:32, email@hidden wrote:
Eliminating the obvious; the file exists at "imagepath" and opens/
loads
correctly. The image on disk is 2592 x 1944. The "size" message
returns 1036.8 x 777.6 consistently on multiple 10.4 machines. Both
iPhoto and Preview show the correct size of the image in pixels.
Apple's Sketch example and my application fail. In a last ditch
effort, I tried the following but both "pixelsWide" and "pixelsHigh"
return zero:
NSImage *image = [[NSImage allocWithZone:[self zone]]
initWithContentsOfFile:imagepath];
NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:[image
TIFFRepresentation]];
Don't do that! You waste space and time and eventually ask the wrong
beast!
What happens: an NSImage (image) is created from a file. It contains
(in case of
a valid jpeg file) exactly one imageRepresentation. From this
representation
TIFF data are created, which are exactly the content of a TIFF image
file.
(A jpeg image is converted into a TIFF image.)
These TIFF data are finally converted into a new imageRepresentation
(bitmap)
which is more or less the same as:
bitmap = [[image representations] objectAtIndex:0];
--
Heinrich Giesen
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden