views:

89

answers:

2

Hi,

I think i'm missing something really basic here. If I do this with a legal URL/path which I know exists:

NSImage* img = [[NSImage alloc] initWithContentsOfFile:[[selectedItem url] path]];
NSLog(@"Image width: %d height: %d", [img size].width, [img size].height);

then I get reported to the console that the width is -2080177216 and the height 0. Although I know that the width is actually 50 and the height 50. I tried calling isValid and it returns YES, and I also tried checking the size of the first representation and it returned the same messed up values. How come the image is not loading properly?

A: 

Does this help?

setSize:

Sets the width and height of the image.

- (void)setSize:(NSSize)aSize

Discussion:

The size of an NSImage object must be set before it can be used. If the size of the image hasn’t already been set when an image representation is added, the size is taken from the image representation's data. For EPS images, the size is taken from the image's bounding box. For TIFF images, the size is taken from the ImageLength and ImageWidth attributes.

Changing the size of an NSImage after it has been used effectively resizes the image. Changing the size invalidates all its caches and frees them. When the image is next composited, the selected representation will draw itself in an offscreen window to recreate the cache.

Availability Available in Mac OS X v10.0 and later. See Also

cool_me5000
I tried retrieving the pixelsWide and pixelsHeight from the image rep, and using them to make a new size and set it on the NSImage. However, after calling setSize with 50x50 on the NSImage, if I check the size I get back from it it still returns the screwed up size!!
evilfred
`initWithContentsOfFile` will set the size. The problem is logging with `%d`.
walkytalky
+6  A: 

The size method returns an NSSize, a struct whose width and height members are of type float. You're treating them as int. Use %f and everything should be fine.

walkytalky
+1 don't you wish there were a `%anything` substitution that would just log the right thing?
Dave DeLong
@Dave That would be too easy!
walkytalky
thank you. yes there should be an anything like in C#!
evilfred
Try this: `JA_DUMP(img.size);` http://jens.ayton.se/blag/almost-elegant-cave-man-debugging/
Ahruman