项目中有一处使用了第三方的图片服务器,当加载列表时,加载图片略缩图,点击略缩图后查看原图,
需求比较简单,就不使用第三方库,直接用GCD实现了
- (void)setupWebImage {
[SVProgressHUD showWithStatus:@"正在加载..."];
//
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// 下载图片
if (!self.imgUrl) { return ;}
NSData *imgData = [NSData dataWithContentsOfURL:self.imgUrl];
//
if (imgData) {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
UIImage *img = [UIImage imageWithData:imgData];
if (img.size.width > SCREEN_WIDTH || img.size.height > SCREEN_HEIGHT) { // 图片宽高超出
self.exampleImgV.size = self.view.size;
} else { // 图片宽高未超出显示原图大小
self.exampleImgV.size = img.size;
}
self.exampleImgV.center = self.view.center;
self.exampleImgV.image = img;
[SVProgressHUD dismiss];
});
} else {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
[SVProgressHUD qy_showErrorWithStatus:@"加载失败\n请稍后重试..."];
});
}
});
}
网友评论