【问题标题】:UIImageView memory leak crash issue - Objective CUIImageView内存泄漏崩溃问题-Objective C
【发布时间】:2013-02-04 01:46:14
【问题描述】:

我正在尝试使用从服务器加载的图像缩略图构建我的应用程序。图像数量众多。因此,当我尝试在我的 iPod 中运行时,它会在加载 50 到 60 张图像后崩溃。崩溃是由于我通过使用仪器测试我的应用程序而了解到的内存泄漏。我为每个 imageView 使用了 imageViews 和一个按钮,并且我还释放了这些对象。这是我在我的应用程序中使用的代码。

NSInteger result = [self loadedBlipCount] + (_feed.hasMore ? 1 : 0);


if (result == 0 && _emptyFeedCell)
    result = 1;
int outer_count = result/3;


scroll_view.backgroundColor = [UIColor whiteColor];

scroll_view.delegate = self;

for (int i=0; i<outer_count; i++)
//if (i<outer_count)

{
    xPos = 10;

    for (int j=0; j<3; j++) {


        _blipyy = [_feed blipAtIndex:count];


       image_view = [[URLImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 90, 90)];

        image_view.tag = count_tag;
        image_view.url = _blipyy.image_tab_images;



        UIButton *img_butt = [[UIButton alloc] initWithFrame:CGRectMake(xPos, yPos, 90, 90)];
        img_butt.tag = count_tag + 10000;
        [img_butt addTarget:self action:@selector(image_tapped:) forControlEvents:UIControlEventTouchUpInside];

        xPos = xPos + 95;
        count = count + 1;
        count_tag = count_tag + 1;
        //count_1= count_1 +1;

        [scroll_view addSubview:image_view];
        [scroll_view addSubview:img_butt];
        [image_view release];
        [img_butt release];
        //[image_view release];
    }
    // });
    yPos = yPos + 95;

}

请帮我解决这个问题。提前致谢!!

【问题讨论】:

  • 使用表格视图来避免内存分配问题
  • 您将保留图像的副本,因为将 image_view 作为子视图添加到 scroll_view。 addSubview 确实保留了它。
  • @HermannKlecker 所以再发布一次 image_view 就能解决问题??
  • 不,这会导致访问冲突。适当的内存管理会有所帮助。这意味着分发那些当前对用户不可见的图像视图。但是,您应该为此使用表格视图或集合视图,而不是重新发明轮子。并按照给出的答案中的链接进行操作。
  • 仅供参考,出于测试目的,我曾经尝试在自己的 scrollView 中加载大约 1000 张或更多尺寸很大的图像,但这并没有导致我的 ipad1 出现任何崩溃。跨度>

标签: iphone ios objective-c memory-leaks uiimageview


【解决方案1】:

使用您自己的实现并重用单元格,而不是滚动视图采用 UITableView 并自定义 UITableViewCell。它可以在没有任何内存问题的情况下工作。

【讨论】:

    【解决方案2】:

    这个问题需要你改变你的方法。使用UITableView 对您来说很重要(它将帮助您处理内存问题)。但是,如果您仍想继续使用UIImageView,那么我建议您使用此AsyncImageView。要调用此 API:

    ASyncImage *img_EventImag = alloc with frame;
    NSURL *url = yourPhotoPath;
    [img_EventImage loadImageFromURL:photoPath];
    [self.view addSubView:img_EventImage]; // In your case you'll add in your TableViewCell.
    

    与使用 UIImageView 相同。很简单,它可以为您完成大部分工作。 AsyncImageView 包括 UIImageView 上的一个简单类别,用于在 iOS 上异步加载和显示图像,以便它们不会锁定 UI,以及用于更高级功能的 UIImageView 子类。 AsyncImageView 与 URL 一起使用,因此它可以与本地或远程文件一起使用。

    加载/下载的图像缓存在内存中,并在出现内存警告时自动清理。 AsyncImageView 独立于 UIImage 缓存运行,但默认情况下,位于应用程序包根目录中的任何图像都将存储在 UIImage 缓存中,从而避免缓存图像的任何重复。

    该库还可用于独立于 UIImageView 加载和缓存图像,因为它提供对底层加载和缓存类的直接访问。

    【讨论】:

      【解决方案3】:

      试试 SDWebImage - 它非常适合我的所有项目,而且易于集成和使用!

      https://github.com/rs/SDWebImage

      【讨论】:

        猜你喜欢
        • 2013-02-07
        • 1970-01-01
        • 2011-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多