【问题标题】:I didn't not find leaks on my UIImage我没有在我的 UIImage 上发现泄漏
【发布时间】:2011-05-04 10:41:09
【问题描述】:

我认为我的 UIImage 引起了一些泄漏。事实上,有很多图片被下载,我认为当我下载新图片时,它显示在上面是旧的,旧的不是dealloc或release...

// 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 个,但较旧的仍然在手机屏幕上.. 显然...

如果屏幕上有更多的图片,我该如何释放旧的图片???

感谢阅读我的问题!!!

【问题讨论】:

    标签: iphone uiimageview uiimage memory-leaks


    【解决方案1】:
    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

    【讨论】:

      【解决方案2】:

      你是 alloc imageView 并且没有释放它。

      所以添加到视图后释放它。

      [imageView release];
      

      【讨论】:

      • Bhalara 如果在 view.addSubView:imageView 之后释放 ImageView,结果和以前一样,显示很多图像
      • 那么最好将 imageView 设为全局,然后只更改图像。
      • @Cheltan Bhalara :好的,我做到了,但是 [imageView setImage:image] 或 [imageView initWithImage:image] 没有正确替换行 imageView = [[UIImageView alloc] initWithImage:image]; ...
      【解决方案3】:

      而不是每次连接完成时都创建一个新的 imageView,你应该只设置 imageView 的 image 属性

      【讨论】:

      • 感谢您的帮助!所以我可以把 UIImageView 作为一个属性,我怎样才能改变这一行: UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; ??
      • 你将 UIImageView *imageView 属性设置为你的类。然后你可以编码:self.imageView.image = image;
      • 好的,谢谢,我做到了,但没有显示图像。我使用 self.imageView.image = image;在我使用 imageView = [[UIImageView alloc] initWithImage:image];
      • 您确定它与您创建并设置为子视图的 imageView 相同吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2012-01-11
      相关资源
      最近更新 更多