简体   繁体   English

UIImageView内存泄漏崩溃问题-目标C

[英]UIImageView memory leak crash issue - Objective C

I am trying to build up my app with the images thumbnails which I loaded from server. 我正在尝试使用从服务器加载的图像缩略图来构建我的应用程序。 The images are in large number. 图像数量很多。 So when I try to run in my iPod it crashes after loading some 50 to 60 images. 因此,当我尝试在iPod中运行时,加载约50到60张图像后它会崩溃。 And the crash is because of the memory leak that I came to know by testing my app with instruments. 崩溃是由于我通过使用仪器测试我的应用程序而知道的内存泄漏。 I have used imageViews and a button for each imageView, and I also released those objects. 我使用了imageViews和每个imageView的按钮,并且还释放了这些对象。 Here is my code that I have used in my app. 这是我在应用程序中使用的代码。

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;

}

Please help me with this issue. 请帮我解决这个问题。 Thanks in Advance!! 提前致谢!!

Instead of scroll View take UITableView and customize UITableViewCell with your own implementation and re use cells. 不用滚动视图,而是使用UITableView并使用自己的实现自定义UITableViewCell并重新使用单元格。 It will work with out any memory issue. 它可以解决任何内存问题。

Try SDWebImage - it's works perfect for all my projects and so easy to integrate and use! 尝试SDWebImage-它非常适合我的所有项目,并且易于集成和使用!

https://github.com/rs/SDWebImage https://github.com/rs/SDWebImage

This problem needs you to change your approach. 此问题需要您更改方法。 It's important for you to use either UITableView (It'll help you handling memory issues). 使用UITableView对您来说很重要(它将帮助您处理内存问题)。 However, if you still want to go ahead and use UIImageView then I'd suggest you to use this AsyncImageView . 但是,如果您仍然想继续使用UIImageView那么建议您使用此AsyncImageView To call this API: 调用此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.

It's same as using UIImageView. 与使用UIImageView相同。 Easy and it does most of the things for you. 简单易行,它可以为您完成大部分工作。 AsyncImageView includes both a simple category on UIImageView for loading and displaying images asynchronously on iOS so that they do not lock up the UI, and a UIImageView subclass for more advanced features. AsyncImageView包括UIImageView上的一个简单类别(用于在iOS上异步加载和显示图像,以使它们不会锁定UI)以及UIImageView子类(用于更高级的功能)。 AsyncImageView works with URLs so it can be used with either local or remote files. AsyncImageView使用URL,因此可以与本地或远程文件一起使用。

Loaded/downloaded images are cached in memory and are automatically cleaned up in the event of a memory warning. 加载/下载的图像会缓存在内存中,并在出现内存警告时自动清除。 The AsyncImageView operates independently of the UIImage cache, but by default any images located in the root of the application bundle will be stored in the UIImage cache instead, avoiding any duplication of cached images. AsyncImageView的运行独立于UIImage缓存,但是默认情况下,位于应用程序捆绑包根目录中的所有图像都将存储在UIImage缓存中,从而避免了缓存图像的任何重复。

The library can also be used to load and cache images independently of a UIImageView as it provides direct access to the underlying loading and caching classes. 该库还可以用于独立于UIImageView加载和缓存图像,因为它提供对基础加载和缓存类的直接访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM