Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 3.0 on disk image caching

Is it possible in AFNetworking to cache image on disk for more than session? For example for a week or month. In my project i used SDWebImage and AFNetworking, but few days ago i found that AFNetworking has the same functionality as SDWebImage, so a removed SDWebImage and wrote such code to store 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 to use in future

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

Get downloaded image

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

But all this working only per session, after i loaded all images i turn off internet and restart my app, result - no images in cache.

Also i try to add such code 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 session. (Sorry for my english)

like image 453
Roman Simenok Avatar asked Mar 28 '16 20:03

Roman Simenok


1 Answers

Images are cached on disk using standard NSURLCache built in iOS, meaning that if your responses include proper cache headers, those items should end up on disk in the cache. So for example headers should look like:

"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 have downloaded an image and want to get it from the disk cache do:

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                                      cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                  timeoutInterval:60];
UIImage *image = [UIImage imageWithData:[[AFImageDownloader defaultURLCache] cachedResponseForRequest:imageRequest].data];
like image 113
Roman Simenok Avatar answered Nov 09 '22 09:11

Roman Simenok