PDA

View Full Version : image setSize changing other images size???




MythicFrost
Feb 7, 2012, 08:01 PM
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?



chown33
Feb 7, 2012, 09:11 PM
NSImage imageNamed: caches images.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsimage_Class/Reference/Reference.html

kainjow
Feb 8, 2012, 11:20 AM
Every time you do
NSImage *myImage = [NSImage imageNamed:@"MyImage"];
Do this instead
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.

MythicFrost
Feb 9, 2012, 06:16 AM
NSImage imageNamed: caches images.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsimage_Class/Reference/Reference.html
I see! Thank you.
Every time you do
NSImage *myImage = [NSImage imageNamed:@"MyImage"];
Do this instead
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?

robbieduncan
Feb 9, 2012, 06:41 AM
Thanks a lot, out of curiosity does imageNamed in iOS cache images too?

Yes. I'm reasonably sure the documentation even says that.

MythicFrost
Feb 9, 2012, 07:22 AM
Yes. I'm reasonably sure the documentation even says that.
Thanks a lot :)