iOS - Caching and loading images asynchronously

2019-01-21 00:34发布

I want an image loading and caching library for iOS that

  • 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.

I've looked at HJCache, but it only satisfies the first two of these criteria. Is there something better?

3条回答
小情绪 Triste *
2楼-- · 2019-01-21 00:45

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!)


ASIHTTPRequest does all of that :)

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 . . .

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-21 00:48

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.

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).

The project is public on https://github.com/toptierlabs/ImageCacheResize. Feel free to use it!

查看更多
别忘想泡老子
4楼-- · 2019-01-21 01:04

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.

I take it you've considered the Three20 library? I don't think it would cover your requirements though.

查看更多
登录 后发表回答