0


I'm trying to cache some images loaded from a URL by using AFImageCache, without success... I'm not sure If i'm doing anything wrong and would love your opinions if you've ever used this library.

I'm getting an image from a NSURLRequest, and then caching it as follows

UIImage *img; //This image is a png icon returned from the server
NSData *imgData = UIImagePNGRepresentation(img); // Always checking length to make sure its more than 0, and it is

[[AFImageCache sharedImageCache] cacheImageData: imgData
                                         forURL: myURL
                                      cacheName: @"IconCache"];

On the next run, i'll try to check if the image was cached, but will always get nil for some reason.

UIImage *myIcon = [[AFImageCache sharedImageCache] cachedImageForURL: myURL cacheName: @"IconCache"];

NSLog("%@", myIcon); // output (null)

Any ideas or thoughts about this? Thanks.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83

1 Answers1

5

What version of AFNetworking are you using? AFImageCache was removed from the public API a long time ago. If you want to do image cacheing, use NSURLCache, which caches all network responses on a system level. AFImageCache has always just been a temporary cache used to improve the performance of image loading in scroll views.

mattt
  • 19,544
  • 7
  • 73
  • 84
  • Hey matt :) I didn't upgrade AFNetworking for a while, mainly because I didn't have any problems with it so far for my needs. Ill give NSURLCache a try. Thank you. – Shai Mishali May 08 '12 at 20:18
  • Hey matt, the thing is AFImageCache allowed me to cache the data by the url, which would allow me to later check if the url was already cached. Is this possible with NSURLCache? – Shai Mishali May 13 '12 at 21:57
  • Yes, `NSURLCache` allows you to check. But honestly, there aren't many occasions where this is a Good Idea™. Also, AFImageCache is private now, so there's no way to interact with it, like to check if cached images exist. Basically, my advice is to ignore it completely. – mattt May 22 '12 at 16:10
  • In my specific situation I have a UITableView, and while the user is scrolling I only load and show the image if he stops scrolling. The thing is , If the image was already cached, I could just show the image while he scrolls since it doesn't take any traffic. How does NSURLCache allow you to check for a specific url ? – Shai Mishali May 22 '12 at 18:48
  • 1
    I strongly advise against what you're describing, if I understand correctly--loading images as the user scrolls is a feature, and something AFN excels at, in that it doesn't (or shouldn't, anyway), cause any performance hiccups. To answer your question, though, `cachedResponseForRequest:` is the method you're looking for. – mattt May 22 '12 at 22:37
  • Thanks for the broad explanation Matt, I appreciate it a lot :) It does cause a performance hiccup when I would be loading each item the user "scrolls over". If I can know which of them was already cached I can make my consideration as to which content should be already visually loaded and which shouldn't. Anyways I've gone with a different approach eventually but its good to know about `cachedResponseForRequest:` anyways . Thank you! :) – Shai Mishali May 22 '12 at 23:15
  • Make sure you upgrade to the latest AFN if you're getting that performance hit. What version are you using? – mattt May 23 '12 at 01:27
  • I just upgraded to the latest branch a couple of days ago :) – Shai Mishali May 23 '12 at 15:55
  • I just published a blog post about caching in AfNetworking that goes over AFImageCache & NSURLCache, if anyone is confused http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/ – vfxdrummer Mar 14 '14 at 18:44