UIImageView setImage Leaks?
Have been searching for a solution to this problem for endless hours now. The problem is really simple. I have a UIImageView
in the nib, i'm loading a image from internet and sets the UIIImageView
with the image. I'm releasing the viewController. Dealloc is actually getting called(!) and loads the viewController and the image again. This can be done by entering background mode really easy. The Instrument Leaks doesn't report any leaks, BUT it shows in allocation that it keeps the image in the memory and it keeps growing.
Basic example:
-(id)init {
if((self = [super init])) {
id path = [NSString stringWithFormat:@"http://www.aSite.com/largeImage.jpg"];
NSData* urlData = [[NSData alloc] initWithC开发者_StackOverflow中文版ontentsOfURL:url];
UIImage* img = [[UIImage alloc] initWithData:urlData];
[background setImage:img];
[urlData release];
[img release];
}
return self;
}
-(void)dealloc {
[background release];
[super dealloc];
}
Some people say that UIImageView actually leaks, or actually CGImage. Some people say that Instrument aren't showing correctly. I'm getting Memory Warning after 10-15 times of doing this with a 2.5mb large image. Results are from real device and latest iOS (or atleast 4-5 weeks ago). As UIImageView is used by so many people i thought it would be easy to find the problem or get a fix from apple?
Source of CGImage Leak: (iphone) UIImageView setImage: leaks?
EDIT: I was abit tierd when i wrote the example. Example is correct now. I have also tried with autoreleased object and same "leak" is still there. Please answer the question if you gonna write an answer.
release
the url after setting the image
[background setImage:[UIImage imageWithData:urlData]];
[urlData release];
[img release];
In the following code you are making some mistakes.
[urlData release];
[background setImage:[UIImage imageWithData:urlData]];
you should use
if((self = [super init])) {
id path = [NSString stringWithFormat:@"http://www.aSite.com/largeImage.jpg"];
NSData* urlData = [[NSData alloc] initWithContentsOfURL:url];
UIImage* img = [[UIImage alloc] initWithData:urlData];
[background setImage:img];
[urlData release];
[img release];
}
Can't you replace
[background setImage:[UIImage imageWithData:urlData]];
with
[background setImage:img];
UPDATE
I think this should also help
if((self = [super init])) {
id path = [NSString stringWithFormat:@"http://www.aSite.com/largeImage.jpg"];
NSData* urlData = [[NSData alloc] initWithContentsOfURL:url];
[background setImage:[UIImage imageWithData:urlData]];
[urlData release];
}
Have you tried:
-(id)init {
if((self = [super init]))
{
[background setImage:
[UIImage imageWithData:
[NSData dataWithUrl:
[NSUrl urlWithString:
@"http://www.aSite.com/largeImage.jpg" ]]]
];
}
return self;
}
-(void)dealloc {
[super dealloc];
}
clean and with no memory leaks!
精彩评论