AFNetworking 3.0 for disk image caching

Is it possible in AFNetworking to cache an image on disk more than a session? For example, during the week or month. In my project, I used SDWebImage and AFNetworking , but a few days ago I found that AFNetworking has the same functionality as SDWebImage , so the remote SDWebImage wrote this code to store the image on disk:

for imageview

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [imageView setImageWithURLRequest:imageRequest placeholderImage:placeholderImage success:nil failure:nil]; 

Download image for future reference

 NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [[AFImageDownloader defaultInstance] downloadImageForURLRequest:imageRequest success:nil failure:nil]; 

Get uploaded image

 NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; UIImage *image = [[AFImageDownloader defaultInstance].imageCache imageforRequest:imageRequest withAdditionalIdentifier:nil]; 

But all this works only for the session, after I downloaded all the images, I turned off the Internet and restarted the application, the result was no images in the cache.

I am also trying to add code like this in AppDelegate :

 NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024 diskCapacity:100 * 1024 * 1024 diskPath:nil]; [NSURLCache setSharedURLCache:sharedCache]; 

But I still can't cache images for longer than a session. (Sorry for my English)

+5
source share
1 answer

Images are cached on disk using the standard iOS built-in NSURLCache, which means that if your answers include the correct cache headers, these elements must end on disk in the cache. So, for example, the headers should look like this:

 "Accept-Ranges" = bytes; "Cache-Control" = "max-age=604800"; Connection = close; "Content-Length" = 3808; "Content-Type" = "image/png"; Date = "Tue, 29 Mar 2016 19:55:52 GMT"; Etag = "\"5630989d-ee0\""; Expires = "Tue, 05 Apr 2016 19:55:52 GMT"; "Last-Modified" = "Wed, 28 Oct 2015 09:42:53 GMT"; Server = nginx; 

also if you upload an image and want to get it from the disk cache:

 NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; UIImage *image = [UIImage imageWithData:[[AFImageDownloader defaultURLCache] cachedResponseForRequest:imageRequest].data]; 
+6
source

Source: https://habr.com/ru/post/1245936/


All Articles