Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,944
40
Australia
I'm new to Mac programming, but not to iPhone programming. I've got an NSButton with an image on it, and since I was unable to find any commands to scale it to the buttons size, I call [object.image setSize:] to make it the size of the button so it fits properly.

However later on, I get another instance of the image from [NSImage imageNamed:] and use [image setSize:] on that image (which is the same image, but not the same instance of it), which changes the size of the image on the button.

I'm quite confused to be honest. What's happening here?
 
Every time you do
Code:
NSImage *myImage = [NSImage imageNamed:@"MyImage"];
Do this instead
Code:
NSImage *myImage = [[[NSImage imageNamed:@"MyImage"] copy] autorelease];
As chown33 said, imageNamed: caches images, so if you need to modify that image work on a copy so you're not modifying the cached image that could be used elsewhere.
 
I see! Thank you.
Every time you do
Code:
NSImage *myImage = [NSImage imageNamed:@"MyImage"];
Do this instead
Code:
NSImage *myImage = [[[NSImage imageNamed:@"MyImage"] copy] autorelease];
As chown33 said, imageNamed: caches images, so if you need to modify that image work on a copy so you're not modifying the cached image that could be used elsewhere.
Thanks a lot, out of curiosity does imageNamed in iOS cache images too?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.