用多线程下载网络图片会导致重复下载浪费流量的问题。
以免出现以上情况,用多线程下载图片方法可以用以下方法。
1.创建一个字典,如果下载了该图片则将用图片地址为key,图片为value保存。
2.创建一个字典,如果子线程已开始下载则用图片地址为key,图片地址为value保存。
3.如果正在下载中,显示占位图片
4.如果字典中有下载的图片,则刷新图片
//属性
@interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageViewOutlet; @property (nonatomic, strong) NSMutableDictionary *imageDict; @property (nonatomic, strong) NSMutableDictionary *dispatchDict; @property (nonatomic, assign) dispatch_queue_t globalQueue; @property (nonatomic, assign) dispatch_group_t dispatchGroup; @property (nonatomic, strong) NSData *imageData; @end
- (NSMutableDictionary *)imageDict { if (!_imageDict) { _imageDict = [NSMutableDictionary dictionary]; } return _imageDict; } - (NSMutableDictionary *)dispatchDict { if (!_dispatchDict) { _dispatchDict = [NSMutableDictionary dictionary]; } return _dispatchDict; } - (dispatch_queue_t)globalQueue { if (!_globalQueue) { _globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); } return _globalQueue; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self dispatchGroupAsync]; }
- (void)dispatchGroupAsync { dispatch_group_t dispatchGroup = dispatch_group_create(); NSString *imageName = @"http://pic29.nipic.com/20130424/11921802_221046126000_2.jpg"; UIImage *image = self.imageDict[imageName]; //字典中没有图片 if (image == nil) { NSLog(@"image is nil"); NSString *dispatchName = self.dispatchDict[imageName]; //也没下载中 if (dispatchName == nil) { NSLog(@"dispatch is nil"); dispatch_group_async(dispatchGroup, self.globalQueue, ^{ //添加线程字典中 self.dispatchDict[imageName] = imageName; NSURL *url = [NSURL URLWithString:imageName]; self.imageData = [NSData dataWithContentsOfURL:url]; }); dispatch_group_notify(dispatchGroup, self.globalQueue, ^{ NSLog(@"dispatch_group_notify queue finished!"); [self.dispatchDict removeObjectForKey:imageName]; self.imageDict[imageName] = self.imageData; [self performSelectorOnMainThread:@selector(refreshImage) withObject:nil waitUntilDone:YES]; }); } //还在下载中 else { self.imageViewOutlet.image = [UIImage imageNamed:@"dufault.jpg"]; } } //有已经下载图片 else { NSLog(@"刷新图片"); self.imageViewOutlet.image = [UIImage imageWithData:self.imageDict[imageName] scale:0.0]; } }
//下载图片完成后回到主线程刷新图片
- (void)refreshImage { self.imageViewOutlet.image = [UIImage imageWithData:self.imageData scale:0.0]; }
//运行效果
//为了测试防止重复下载操作,在开始下载时连续猛击多次屏幕
2015-08-08 22:02:18.055 GCD下载图片[2840:163865] image is nil 2015-08-08 22:02:18.055 GCD下载图片[2840:163865] dispatch is nil 2015-08-08 22:02:18.235 GCD下载图片[2840:163865] image is nil 2015-08-08 22:02:18.403 GCD下载图片[2840:163865] image is nil 2015-08-08 22:02:18.560 GCD下载图片[2840:163865] image is nil 2015-08-08 22:02:18.704 GCD下载图片[2840:163865] image is nil 2015-08-08 22:02:18.874 GCD下载图片[2840:163960] dispatch_group_notify queue finished! 2015-08-08 22:02:18.890 GCD下载图片[2840:163865] 刷新图片 2015-08-08 22:02:19.495 GCD下载图片[2840:163865] 刷新图片
---end
注:如果想了解用NSOperation来下载,请查看http://blog.csdn.net/zhangwenhai001/article/details/46723597
版权声明:本文为博主原创文章,转载请注明来源:http://blog.csdn.net/zhangwenhai001
原文地址:http://blog.csdn.net/zhangwenhai001/article/details/47362719