如何从远程服务器下载无限数量的图像并在UICollection View / UITableView中显示图像?

时间:2022-03-25 03:41:18

I want to display the infinite images in UITableView/UICollectionview, here the images will be received from remote server, I've done this by using GCD but it causes Memory issues and app got crash.Please help to fix it. Also I noticed some images aren't deallocated. Here is piece of code I've used to download images.

我想在UITableView / UICollectionview中显示无限图像,这里将从远程服务器接收图像,我通过使用GCD完成此操作,但它导致内存问题和应用程序崩溃。请帮助修复它。另外我注意到一些图像没有被释放。这是我用来下载图像的一段代码。

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        NSData *imgData = UIImageJPEGRepresentation(image, 0.1);
        UIImage *image1 = [UIImage imageWithData:imgData];
        if (image1.size.width != 130 || image1.size.height != 100)
            {
                CGSize itemSize = CGSizeMake(130, 100);
                UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0f);
                CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                [image1 drawInRect:imageRect];
                image1  = UIGraphicsGetImageFromCurrentImageContext();
                [self setImage:image1 forKey:[url absoluteString]];
                //  NSLog(@" down Size of Image(bytes):%d",[imgData length]);
                UIGraphicsEndImageContext();

            }

        dispatch_async(dispatch_get_main_queue(), ^{
            completion(image1);
            //image1=nil;
        });
    });

3 个解决方案

#1


you can work with: AFNetworking+UIImageView

你可以使用:AFNetworking + UIImageView

About the memory issues and crashes, be careful with the retain cycles, use the profiler/Instruments, if you want to continue with your code is good idea to change:

关于内存问题和崩溃,请注意保留周期,使用profiler / Instruments,如果要继续使用代码,最好更改:

__weak MyClassViewOrViewController* weakSelf = self;      
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    ...
    __strong MyClassViewOrViewController *strongSelf = weakSelf;
    [strongSelf setImage:image1 forKey:[url absoluteString]];
    ...
});

Thanks,

J.

#2


You can do using LazyLoading Concept

您可以使用LazyLoading Concept

Reference: https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

#3


You can use SDWebImage to load infinite image, good performance, cache..

您可以使用SDWebImage加载无限图像,良好的性能,缓存..

#1


you can work with: AFNetworking+UIImageView

你可以使用:AFNetworking + UIImageView

About the memory issues and crashes, be careful with the retain cycles, use the profiler/Instruments, if you want to continue with your code is good idea to change:

关于内存问题和崩溃,请注意保留周期,使用profiler / Instruments,如果要继续使用代码,最好更改:

__weak MyClassViewOrViewController* weakSelf = self;      
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    ...
    __strong MyClassViewOrViewController *strongSelf = weakSelf;
    [strongSelf setImage:image1 forKey:[url absoluteString]];
    ...
});

Thanks,

J.

#2


You can do using LazyLoading Concept

您可以使用LazyLoading Concept

Reference: https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

#3


You can use SDWebImage to load infinite image, good performance, cache..

您可以使用SDWebImage加载无限图像,良好的性能,缓存..