Can anyone tell me how to release the image which is used as background image in UIButton. I created Custom button using following code but i could not able to release the memory of UIImage which is set a background image in the UIButton. Here is code which i used to create UIButton
`
for (int index=0;index<10; index++)
{
UIImage *image=[[UIImage alloc] initWithData:imageData];
CGRect frame = CGRectMake(xValue, 10, 50, 50);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:@selector(onClickImgButton:)forControlEvents:UIControlEventTouchUpInside];
[button setTag:index];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setImage:image forState:UIControlStateNormal];
[button.layer setCornerRadius:10.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:3.0f];
[button.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
image=nil;
[image release];
[horizontalScrollView addSubview:button];
button=nil;
[button release];
xValue=xValue+60;
cx +=60;
}
`
First, you mixed your
image=nil;
[image release];
order, you should first release, than set the pointer to nil. Right now you’re sending “release” to nil and nothing happens. You’re doing the same for the button btw.
Now if you want the image to be freed from memory, you can’t do it as long as it’s being used by the button. So you’ll have to either free the button from memory (in this case by fixing the release and setting the pointer to nil, and removing the button from it’s superview) or do
Should set the retain count to 0 and eventually dealloc the image