background image for UIButton

Discussion in 'iPhone/iPad Programming' started by jagatnibas, Nov 15, 2009.

  1. macrumors regular

    Joined:
    Jul 28, 2008
    #1
    i am doing something like below

    Code:
    UIImage *img = [UIImage imageNamed:@"a.png"];
    UIButton *btn =[[valid instance from nib file]]
    
    [btn setbackgroundImage:img forState:UIControlStateNormal];
    [img release];
    
    shouldnot i be able to release img ?
    because i dont need the image anymore i ca release img, isn;t it ?

    in UIImageView we are able to do that, in button why not ?

    like in

    Code:
    UIImageView = alloc
    setImage:img
    add subview: imgview
    [img release];
    [imgview release];
    
    it just works fine !

    regards
    jagat
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Go and read the memory management guide: you should not release img as you do not create it with an alloc/init or copy constructor so it is autoreleased.
     
  3. macrumors newbie

    Joined:
    May 24, 2009
    Location:
    India
    #3
    Normally it releases the image object.:rolleyes:
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Normally what releases the object? That makes no sense.
     
  5. macrumors newbie

    Joined:
    May 24, 2009
    Location:
    India
    #5
    If we do

    Code:
    UIImage *img = [UIImage imageNamed:@"1.png"];
    [img release];
    
    Because after assigning it you dont need the object of UIImage.

    If you are more interested in memory management (UIImage) please go through this link i hope it helps because it helped me a lot. :)

    http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/

    :)
     

Share This Page