开发者

Do we need to release an UIImage object even not allocated memory?

开发者 https://www.devze.com 2022-12-24 13:41 出处:网络
I added an image to button UIImage* deleteImage = [UIImage imageNamed:@\"Delete.png\"]; CGRect imageFrame=CGRectMake(-4,-4, 310, 55);

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.


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.


"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/

0

精彩评论

暂无评论...
验证码 换一张
取 消