views:

180

answers:

2

Hi Guys,

I added an image to button

UIImage* deleteImage = [UIImage imageNamed:@"Delete.png"];
CGRect imageFrame=CGRectMake(-4,-4, 310, 55);
[btn setFrame:imageFrame];
btn.backgroundColor=[UIColor clearColor];
[btn setBackgroundImage:deleteImage forState:UIControlStateNormal];
[btn setTitle:@"Delete" forState:UIControlStateNormal];
[btn addTarget:self  action:@selector(editDeleteAction) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:btn];
[deleteImage release];// do we need to release the image here

If I release here its working fine but in object allocations no.of image count is increasing.

+1  A: 

If you create an image with the imageNamed: message you don't have to release it, because you get an autoreleased image back.

Only if you create the image with one of the init...: messages you have to release it later on.

schaechtele
That is the case with ANY object that you instantiate with `init` - you own it, therefore you must release it.
Jacob Relkin
Its more a case of any object that is created new (not necessarily instantiated) using any of the CARN rule methods. That is to say that any methods that contain the words Copy, Alloc, Retain or New should return an object with a retain count of +1 and are owned by the caller and should be released at some point. Anything else is assumed to return an autoreleased object.
Jasarien
For further information look at the iPhone memory management guide in the apple documentation:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/
schaechtele
Thanks for your response
Madan Mohan
A: 

"imageNamed:" method gives an autoreleased object, so you don't have to release object.

FYI: "imageNamed:" method uses an internal cache (you may face memory warning if imageNamed: method used extensively). It is better to remove cache on receiving warnings.

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [[ImageCache sharedImageCache] removeAllImagesInMemory];
}

Go through this guide http://akosma.com/2009/01/28/10-iphone-memory-management-tips/

Chandan Shetty SP
The imageNamed: cache problem has been solved since iPhone OS 3.0. It is, however, a real issue if your app has to work with any of the 2.x versions...
Adrian Kosmaczewski