iOS异步加载缓存图片
2015-10-08 18:50
405 查看
- (void)viewDidLoad {
[super
viewDidLoad];
imageView = [[UIImageView
alloc] init];
[imageView
setBackgroundColor:[UIColor
grayColor]];
imageView.frame =
CGRectMake(60,
60, 200,
200);
[self.view
addSubview:imageView];
NSOperationQueue *operationQueue = [[NSOperationQueue
alloc] init];
NSInvocationOperation *op = [[NSInvocationOperation
alloc] initWithTarget:self
selector:@selector(downloadImage)
object:nil];
[operationQueue
addOperation:op];
}
- (void)downloadImage {
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES);
NSString *diskCachePath = [[paths
objectAtIndex:0]
stringByAppendingPathComponent:@"imageCache"];
//如果目录imageCache不存在,创建目录
if (![[NSFileManager
defaultManager] fileExistsAtPath:diskCachePath]) {
NSError *error=nil;
[[NSFileManager
defaultManager] createDirectoryAtPath:diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
NSString *localPath = [NSString
stringWithFormat:@"%@/headimage.png",diskCachePath];
//如果有缓存图片,直接读取cache内的缓存图片
if ([[NSFileManager
defaultManager] fileExistsAtPath:localPath]) {
NSData *data = [NSData
dataWithContentsOfFile:localPath];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage
imageWithData:data];
});
}
else{
NSURL *imagePathUrl = [NSURL
URLWithString:@"http://s15.sinaimg.cn/middle/9914f9fdhbc6170891ebe&690.png"];
NSData *data = [NSData
dataWithContentsOfURL:imagePathUrl];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage
imageWithData:data];
});
[data
writeToFile:localPath atomically:YES];
}
}
[super
viewDidLoad];
imageView = [[UIImageView
alloc] init];
[imageView
setBackgroundColor:[UIColor
grayColor]];
imageView.frame =
CGRectMake(60,
60, 200,
200);
[self.view
addSubview:imageView];
NSOperationQueue *operationQueue = [[NSOperationQueue
alloc] init];
NSInvocationOperation *op = [[NSInvocationOperation
alloc] initWithTarget:self
selector:@selector(downloadImage)
object:nil];
[operationQueue
addOperation:op];
}
- (void)downloadImage {
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES);
NSString *diskCachePath = [[paths
objectAtIndex:0]
stringByAppendingPathComponent:@"imageCache"];
//如果目录imageCache不存在,创建目录
if (![[NSFileManager
defaultManager] fileExistsAtPath:diskCachePath]) {
NSError *error=nil;
[[NSFileManager
defaultManager] createDirectoryAtPath:diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
NSString *localPath = [NSString
stringWithFormat:@"%@/headimage.png",diskCachePath];
//如果有缓存图片,直接读取cache内的缓存图片
if ([[NSFileManager
defaultManager] fileExistsAtPath:localPath]) {
NSData *data = [NSData
dataWithContentsOfFile:localPath];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage
imageWithData:data];
});
}
else{
NSURL *imagePathUrl = [NSURL
URLWithString:@"http://s15.sinaimg.cn/middle/9914f9fdhbc6170891ebe&690.png"];
NSData *data = [NSData
dataWithContentsOfURL:imagePathUrl];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage
imageWithData:data];
});
[data
writeToFile:localPath atomically:YES];
}
}
相关文章推荐
- iOS:对GCD中 同步、异步、并行、串行的见解
- ios开发,javascript直接调用oc代码
- xcode生成的IOS安装文件的位置
- iOS自适应(代码) 简单介绍一下
- iOS开发知识001
- Xamarin.Ios 下拉菜单,多选,键盘弹出功能
- iOS---iOS9中获取通讯录的方法:Contacts. framework简单使用
- iOS Reverse Engineering Part One: Configuring LLDB
- iOS9 新特性 - Search APIs
- iOS开源库和完整应用
- 详解在IOS后台执行
- [iOS]转:iOS最佳实践
- 升级Xcode7、iOS9之后,如何应对一些奇葩的bug
- iOS 心得三 工作中遇到的相关要点
- [IOS]适配IOS9
- iOS 类型编码(Type Encodings)
- iOS 降级
- wxhl iOS bj 49 张浩 学习心得文章连载 第13章
- iOS ZBarSDK 64位下载
- iOS didMoveToSuperview