码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 用GCD下载网络图片方法

时间:2015-08-08 22:54:38      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ios   图片下载   多线程下载图片   防重复下载   

用多线程下载网络图片会导致重复下载浪费流量的问题。

以免出现以上情况,用多线程下载图片方法可以用以下方法。

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

iOS 用GCD下载网络图片方法

标签:ios   图片下载   多线程下载图片   防重复下载   

原文地址:http://blog.csdn.net/zhangwenhai001/article/details/47362719

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!