【问题标题】:iPhone: Memory used by images is not freediPhone:图像使用的内存未释放
【发布时间】:2011-05-05 13:55:58
【问题描述】:

我在下面有这段代码,可以从网络加载图像。 这些图像在单击表格单元格后显示,并且每次单击表格单元格时都会重新加载。 关键是用“Instruments”分析内存分配,当我从详细视图返回到表格时,图像占用的内存没有被释放。

有人有什么建议吗? 当然,我负责所有案件的发布……但这似乎无济于事。

    NSError *error = nil;
    UIImage *img = nil;
    NSString *myurl = [IMG_SERVER_URL stringByAppendingString: tmp_link.url];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSData *imageData = nil;
    if(myurl!=nil){
        imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl] options:nil error:&error];
        if (error == 0) {
            img = [[UIImage alloc] initWithData:imageData];
        }
    }
    [imageData release]; imageData = nil;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(targetWidth*i, 0, targetWidth, targetHeight)]; 
    imageView.image = img; 
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [img release];
    [....some code....]
    [imageView release];

【问题讨论】:

  • 我已经添加了几行代码,这很有意义,感谢您发现了一个缺失的有用部分。

标签: iphone memory uiimageview memory-management


【解决方案1】:

我认为“imageView.image = img”会增加图像对象的引用计数。如果我是对的,只要您不释放 imageView,分配的内存就不会被释放。

【讨论】:

  • 我已经在代码中稍后发布了 imageView ......我再次更新了代码 sn-p,无论如何感谢您的建议!
猜你喜欢
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多