image setSize changing other images size???

Discussion in 'Mac Programming' started by MythicFrost, Feb 7, 2012.

  1. macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    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?
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    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.
     
  4. thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #4
    I see! Thank you.
    Thanks a lot, out of curiosity does imageNamed in iOS cache images too?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Yes. I'm reasonably sure the documentation even says that.
     
  6. thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #6
    Thanks a lot :)
     

Share This Page