iOS - 异步缓存和加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5078193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - Caching and loading images asynchronously
提问by nornagon
I want an image loading and caching library for iOS that
我想要一个适用于 iOS 的图像加载和缓存库
- loads images asynchronously,
- caches images, with a configurable cache size and LRU behaviour,
- checks to see if images have been updated, using HTTP HEAD,
- doesn't cache anything in the event of an error code or an invalid image.
- 异步加载图像,
- 缓存图像,具有可配置的缓存大小和 LRU 行为,
- 使用 HTTP HEAD 检查图像是否已更新,
- 如果出现错误代码或无效图像,则不会缓存任何内容。
I've looked at HJCache, but it only satisfies the first two of these criteria. Is there something better?
我看过HJCache,但它只满足这些标准中的前两个。有什么更好的吗?
采纳答案by Tony
I know that this thread has been answered, but I have tried a library that has worked great. I was using ASIHttpRequest before and the difference is big.
我知道该线程已得到解答,但我尝试了一个运行良好的库。我之前用过ASIHttpRequest,差别很大。
https://github.com/rs/SDWebImage
https://github.com/rs/SDWebImage
Also, if someone needs to Resize or Crop the remote images, and have the same features that SDWebImage provide, I have integrated SDWebImage library with UIImage+Resize library (by Trevor Harmon), and created an example project. I modified the code of SDWebImage to deal with transformations (crop, resize).
此外,如果有人需要调整或裁剪远程图像,并具有 SDWebImage 提供的相同功能,我将 SDWebImage 库与 UIImage+Resize 库(由 Trevor Harmon 提供)集成,并创建了一个示例项目。我修改了 SDWebImage 的代码来处理转换(裁剪、调整大小)。
The project is public on https://github.com/toptierlabs/ImageCacheResize. Feel free to use it!
该项目在https://github.com/toptierlabs/ImageCacheResize上公开。随意使用它!
回答by deanWombourne
EDIT : ASI has been deprecated now but @Tony's answer has a link to a library that seems to do most of this (disclaimer - I've not yet tried it and am going by @Tony's recommendation!)
编辑:ASI 现在已被弃用,但@Tony 的回答有一个指向一个库的链接,该库似乎完成了大部分工作(免责声明 - 我还没有尝试过,我会按照@Tony 的建议进行操作!)
ASIHTTPRequestdoes all of that :)
ASIHTTPRequest完成所有这些:)
All you would have to do is add something like to the success callback
您所要做的就是在成功回调中添加类似的内容
UIImage *image = [UIImage imageWithData:[request responseData]];
// If the image is nil ASIHTTPRequest will let you remove the data from the cache
// but I can't remember off the top of my head how. Checkout the docs!
EDIT
编辑
And when you've finished it, you could always pop it onto github for the rest of us . . .
当你完成它时,你总是可以为我们其他人把它放到 github 上。. .
回答by occulus
I had a look recently for the sort of thing you describe -- either a general data cache, or an image centric one -- and didn't find much of interest. So you may have to roll your own to get all those features.
我最近查看了您所描述的那种东西——无论是通用数据缓存,还是以图像为中心的缓存——并没有发现太多兴趣。因此,您可能必须自己动手才能获得所有这些功能。
There's a various blogs posts and things detailing such things, such as http://davidgolightly.blogspot.com/2009/02/asynchronous-image-caching-with-iphone.html.
有各种博客文章和详细介绍此类内容的内容,例如 http://davidgolightly.blogspot.com/2009/02/asynchronous-image-caching-with-iphone.html。
I take it you've considered the Three20 library? I don't think it would cover your requirements though.
我想你考虑过 Three20 图书馆吗?我认为它不会满足您的要求。