如何从远程服务器下载无限多张图片并显示在UICollection View/UITableView中?
How to download infinite number of images from remote server and display the images in UICollection View/UITableView?
我想在 UITableView/UICollectionview 中显示无限图像,这里的图像将从远程服务器接收,我使用 GCD 完成了此操作,但它会导致内存问题并且应用程序得到 crash.Please帮助修复它。我还注意到一些图像没有被释放。这是我用来下载图像的一段代码。
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;
});
});
你可以使用 LazyLoading 概念
参考:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html
可以使用SDWebImage加载无限图片,性能好,缓存..
您可以使用:AFNetworking+UIImageView
关于内存问题和崩溃,小心保留周期,使用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]];
...
});
谢谢,
J.
我想在 UITableView/UICollectionview 中显示无限图像,这里的图像将从远程服务器接收,我使用 GCD 完成了此操作,但它会导致内存问题并且应用程序得到 crash.Please帮助修复它。我还注意到一些图像没有被释放。这是我用来下载图像的一段代码。
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;
});
});
你可以使用 LazyLoading 概念
参考:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html
可以使用SDWebImage加载无限图片,性能好,缓存..
您可以使用:AFNetworking+UIImageView
关于内存问题和崩溃,小心保留周期,使用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]];
...
});
谢谢,
J.