Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disk Backed Image Cache for UIImageView in AFNetworking

I'm looking to swap out the AFImageCache used by default in the UIImageView+AFNetworking category for something that's disk based and that can managed a little more accurately (something like NSURLCache). Unfortunately, since UIImageView+AFNetworking is a category and not a subclass, I can't just override af_sharedImageCache with a sublclass of UIImageView OR another category.

Is there any other way to achieve this functionality without copying and pasting most of UIImageView+AFNetworking into my own subclass?

like image 543
jpredham Avatar asked Feb 16 '23 06:02

jpredham


1 Answers

The SDWebImage project provides a similar UIImageView category, but offers both in-memory (using NSCache) and on-disk (using NSFileManager) caching. I'd recommend just using that when you need to cache to disk.

The downside to this implementation is that your network requests won't go through your AFHTTPClient subclass, so depending on what your needs are you might need to implement your own operation queue, authentication, etc. If you're just using it for something basic, like displaying avatar images in a table view, it should be fine.

If that downside bothers you, an alternate idea would be to use SDImageCache (included in the SDWebImage project) to cache the images, and download them yourself using AFNetworking.

Finally, note that AFNetworking has built-in support for NSURLCache, and if you create one it will cache your images to disk. However, image caching is typically used for showing lots of images in a UIScrollView, and NSURLCache doesn't have good enough performance for smooth scrolling.

like image 116
Aaron Brager Avatar answered Mar 04 '23 01:03

Aaron Brager