【发布时间】:2013-03-08 10:34:22
【问题描述】:
我直接在 Imageview 中打开 URL 图像,但我想将图像存储在缓存中以供将来参考...帮我解决我的问题..
提前致谢
【问题讨论】:
-
到目前为止,你写了什么代码(用于保存图像)?在此处发布。
我直接在 Imageview 中打开 URL 图像,但我想将图像存储在缓存中以供将来参考...帮我解决我的问题..
提前致谢
【问题讨论】:
您可以使用使用图像缓存的SDWebImage...
这个库为UIImageVIew 提供了一个类别,支持来自网络的远程图像。
它提供:
一个UIImageView 类别将网络图像和缓存管理添加到 Cocoa Touch 框架中
一个异步图片下载器
具有自动缓存过期处理的异步内存 + 磁盘映像缓存
背景图片解压
保证不会多次下载同一个网址
保证不会一次又一次地重试虚假网址
保证主线程永远不会被阻塞
表演! 使用 GCD 和 ARC
【讨论】:
这是iOS开发中的经典问题,我之前写了一个类OnlineImageView,使用的是NSOperationQueue -- 现在GCD可能更好。
您想访问在线图片:
一些示例代码:
- (void)downloadImage:(NSDictionary *)info
{
/* HOWTO: Prevent Downloading the same url many times */
/* Keep the download task one by one ? */
NSString *url = [info objectForKey:@"url"];
NSURL *imageUrl = [NSURL URLWithString:url];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
if (image) {
id theDelegate = [info objectForKey:@"delegate"];
if ([theDelegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)]) {
[theDelegate imageDownloader:self didFinishWithImage:image];
}
[cacheQueue cacheImage:image withKey:url];
} else {
#ifdef DEBUG
NSLog(@"Failed to download : %@\n", url);
#endif
}
}
【讨论】:
使用 SDWebImage 。它最好和最容易的图像数据缓存。
【讨论】: