找到你要的答案

Q:How to download infinite number of images from remote server and display the images in UICollection View/UITableView?

Q:如何从远程服务器下载图片无限数量和uicollection查看/表格显示图像?

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.

    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;
        });
    });

我想在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;
        });
    });
answer1: 回答1:

you can work with: 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:

__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.

你可以使用:AFNetworking + UIImageView

关于内存问题和崩溃,小心与保留周期,使用分析器/文书,如果你想继续你的代码是好主意改变:

__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]];
    ...
});

谢谢,

J.

answer2: 回答2:

You can do using LazyLoading Concept

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

你可以使用lazyloading概念

参考:https://developer.apple.com/library/ios/samplecode/lazytableimages/introduction/intro.html

answer3: 回答3:

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

你可以使用sdwebimage加载无限的形象,良好的性能,缓存..

ios  objective-c  uitableview  memory-leaks  uicollectionview