一直在寻找这个问题的解决之道。问题真的很简单。我在nib中有一个UIImageView
,我从互联网上加载一个图像,并用图像设置UIIImageView
。我要发布viewController。Dealloc实际上正在被调用(!)并再次加载viewController和图像。这可以通过进入背景模式非常容易做到。仪器泄漏没有报告任何泄漏,但在分配中显示,它将图像保存在内存中,并且不断增长。
基本例子:
-(id)init {
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];
}
return self;
}
-(void)dealloc {
[background release];
[super dealloc];
}
有些人说UIImageView实际上是泄漏的,或者实际上是CGImage。有人说仪器显示不正确。在使用2.5mb大图像执行10-15次后,我会收到内存警告。结果来自真正的设备和最新的iOS (或至少在4-5周前)。由于UIImageView被这么多人使用,我认为很容易找到问题,或者从苹果那里得到解决办法。
CGImage泄漏来源:(iphone) UIImageView setImage: leaks?
编辑:--我写这个例子的时候非常讨厌。例子现在是正确的。我也尝试过用自动释放的对象,同样的“泄漏”仍然存在。如果你要写一个答案,请回答问题。
发布于 2011-05-04 04:52:54
在下面的代码中,您正在犯一些错误。
[urlData release];
[background setImage:[UIImage imageWithData:urlData]];
你应该使用
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];
}
发布于 2011-05-04 04:56:31
设置图像后的release
url
[background setImage:[UIImage imageWithData:urlData]];
[urlData release];
[img release];
发布于 2011-05-04 04:54:53
你就不能代替
[background setImage:[UIImage imageWithData:urlData]];
使用
[background setImage:img];
更新
我认为这也会有帮助
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];
}
https://stackoverflow.com/questions/5878690
复制相似问题