Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.
Alexander Grebenyuk edited this page Sep 27, 2015 · 9 revisions

Caching

GIF


Caching

Is there some additional configuration needed to make cache persist across app launches?

There is a separate memory cache (NSCache) that stores decompressed images while your app is running. And there is a persistent NSURLCache, which is controlled entirely on the system framework level by NSURLSession, DFImageManager doesn't override any of the system behaviors regarding disk caching.

The default image manager initialized NSURLSession with NSURLRequestUseProtocolCachePolicy which enables HTTP cache policy for HTTP or HTTPS requests. There is no additional configuration required to make cache persist across app launched. However, make sure that HTTP caching is implemented properly by your server.

HTTP caching is a universal standard that should be adapted in any possible case. However, if you don't have control over the sever with misconfigured HTTP caching you can force NSURLSession to always use cached results, regardless of age or expiration date. To do that you need to initialize NSURLSession with NSURLRequestReturnCacheDataElseLoad request policy:

NSURLSessionConfiguration *configuration = /* Your configuration, can be copied from DFImageManager+DefaultManager.m */
configuration.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;

DFURLImageFetcher *fetcher = [[DFURLImageFetcher alloc] initWithSessionConfiguration:configuration];
DFImageManager *imageManager = [[DFImageManager alloc] initWithConfiguration:[DFImageManagerConfiguration configurationWithFetcher:fetcher processor:[DFImageProcessor new] cache:[DFImageCache new]]];

[DFImageManager addSharedManager:imageManager];

References

  1. Original Question
  2. RFC 7234. HTTP/1.1 Caching
  3. URL Loading System Programming Guide. Understanding cache control.
  4. When NSURLSession caches responses
  5. Beginners HTTP caching guide #1 and beginners HTTP caching guide #2.

GIF

I use -[DFImageManaging imageTaskWithRequest:completion:] method to request an animated image, how do I display it?`

The image task will return an instance of DFAnimatedImage : UIImage class with a special animatedImage property. The DFAnimatedImage object represents a poster image for the underlying animated image. It is a regular UIImage that doesn't override any of the native UIImage behaviors and it can be used anywhere where a regular UIImage can be used.

The underlying animated image is an instance ofFLAnimatedImage : NSObject class that can be used for GIF playback in the FLAnimatedImageView : UIImageView object. To start playback set the animatedImage property of FLAnimatedImageView. For more info on FLAnimatedImage see FLAnimatedImage library.

The DFAnimatedImageView class uses FLAnimatedImageView for GIF playback.