2011-05-04 82 views
1

我想我有一些泄漏是由我的UIImage造成的。事实上,有很多被下载的图像。我认为,当我下载新的图片,它的表现上较老的和旧的没有的dealloc或释放...我没有发现我的UIImage泄漏

// Create and posit the UIImage 
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
NSLog(@"retain count 1 : %i", [imageView retainCount]); 
imageView = [[UIImageView alloc] initWithImage:image]; 
NSLog(@"retain count 2 : %i", [imageView retainCount]); 
//[imageView setImage:image]; 
//[imageView initWithImage:image]; 
imageView.frame = CGRectMake(0,64,320,367); 
NSLog(@"retain count 3 : %i", [imageView retainCount]); 
[self.view addSubview:imageView]; 
NSLog(@"retain count 4 : %i", [imageView retainCount]); 
[imageView release]; 
NSLog(@"retain count 5 : %i", [imageView retainCount]); 
NSLog(@"-----------------------"); 

该代码给我这一结果:

retain count 1 : 0 
retain count 2 : 0 
retain count 3 : 1 
retain count 4 : 1 
retain count 5 : 2 
retain count 6 : 1 
----------------------- 
retain count 1 : 0 
retain count 2 : 0 
retain count 3 : 1 
retain count 4 : 1 
retain count 5 : 2 
retain count 6 : 1 
----------------------- 
retain count 1 : 0 
retain count 2 : 0 
retain count 3 : 1 
retain count 4 : 1 
retain count 5 : 2 
retain count 6 : 1 
----------------------- 

显然,图像被释放,仅出现1,但仍然较老的手机屏幕上..显然...

如果在屏幕上显示更多的图片,怎么样我可以释放较旧的图片吗?

感谢阅读我的问题!

+0

我已经更新我的代码图像属性! – clement 2011-05-05 12:19:06

回答

0
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.frame = CGRectMake(0,64,320,367); 
[self.view addSubview:imageView]; 
[imageView release]; 
} 

你应该release分配imageView

+0

我必须把[imageView发布];在那些行的第二次调用之前如此? – clement 2011-05-04 10:50:36

0

您是页头ImageView的,而不是将其释放。

因此,在添加以查看后释放它。

[imageView release]; 
+0

Bhalara如果在view.addSubView:imageView之后发布ImageView,结果看起来像之前那样,很多图像被显示 – clement 2011-05-04 11:12:15

+0

那么最好是将imageView作为一个全局图像,然后再改变图像。 – 2011-05-04 11:16:43

+0

@Cheltan Bhalara:好的,我做到了,但[imageView setImage:image]或[imageView initWithImage:image]不能正确替换lineView = [[UIImageView alloc] initWithImage:image]; ... – clement 2011-05-04 13:52:06

0

,而不是每次创建一个新的ImageView您的连接完成后,你应该只设置的ImageView的

+0

感谢您的帮助!所以我可以把UIImageView作为一个事物,我怎样才能改变这一行:UIImageView * imageView = [[UIImageView alloc] initWithImage:image]; ?? – clement 2011-05-04 11:10:28

+0

你使你的类的UIImageView * imageView属性。然后你可以编码:self.imageView.image = image; – Tomen 2011-05-05 12:44:02

+0

好的,谢谢,我做了,但没有显示图像..我使用self.imageView.image = image;在我使用imageView = [[UIImageView alloc] initWithImage:image]之前; – clement 2011-05-05 12:52:25

相关问题