NSString * filepath=xxxxx;
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
//异步加载图片
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage * img=[UIImage imageWithContentsOfFile:filepath];
dispatch_sync(dispatch_get_main_queue(), ^{
self.bgImageView.image=img;
});
});
}else{
[self.bgImageView sd_setImageWithURL:[NSURL URLWithString:imageUrlstr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// 判断本地图片是否存在 不存在写入本地
NSString *typePath=[[ShareMethod appRootPath] stringByAppendingPathComponent:FangAnBrowseFolder];
[FileFunction createFolderAtPath:typePath folderName:solationID];
//异步保存图片
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
BOOL issuc = [UIImagePNGRepresentation(image) writeToFile:filepath atomically:YES];
NSLog(@"issuc==%d",issuc);
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
}];
}
本文介绍了一种iOS应用中图片的缓存与加载方法,包括检查本地文件是否存在,如果存在则从本地加载图片;若不存在,则从网络下载并保存到本地。整个过程使用了GCD进行异步处理,确保UI响应速度。
1258

被折叠的 条评论
为什么被折叠?



