1 // 2 // UIImageView+WebCache.m 3 // 02-SDWebImage 4 // 5 // Created by mac on 16/4/20. 6 // Copyright © 2016年 mac. All rights reserved. 7 // 8 9 #import "UIImageView+WebCache.h" 10 11 @implementation UIImageView (WebCache) 12 13 /** 14 * 注意点:有任务应该开启多线程,把下载放入到非主线程,加载UI必须得放入主线程 15 */ 16 17 - (void)setImageWithURL:(NSURL *)url { 18 19 // NSData *data = [NSData dataWithContentsOfURL:url]; 20 // 21 // UIImage *image = [UIImage imageWithData:data]; 22 // 23 // self.image = image; 24 25 //1。 开启多线程 26 [self performSelectorInBackground:@selector(downloadImage:) withObject:url]; 27 } 28 29 - (void)downloadImage:(NSURL *)url { 30 31 NSData *data = [NSData dataWithContentsOfURL:url]; 32 33 UIImage *image = [UIImage imageWithData:data]; 34 35 //2。 所有和UI相关的操作都应该在主线程中执行 36 [self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; 37 } 38 39 @end
时间: 2024-11-08 05:19:03