GCD网络加载图片

#import "UIImageView+WebCache.h"

@implementation UIImageView (WebCache)

- (void)setImageWithURL:(NSURL *)url {

    //加载网络数据
    __block UIImage *image = nil;
    //创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    //异步添加 加载数据,刷新图片 到串行队列
    dispatch_async(queue, ^{

        NSData *data = [NSData dataWithContentsOfURL:url];
        image = [UIImage imageWithData:data];
        //异步添加 刷新图片 到主队列
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^{

            self.image = image;
        });
    });
}
@end

你可能感兴趣的:(iOS高级)