0

In my code, I am downloading all of the images in the beginning of the application lifecycle:

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];

     NSURL *url = [NSURL URLWithString:[item imagePath]];
     __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     [request setCompletionBlock:^{
     //NSData *responseData = [request responseData];
     //[iw setImage: [UIImage imageWithData:responseData] forState:UIControlStateNormal];   
}];

[request setFailedBlock:^{
     // NSError *error = [request error];
     //[iw setImage:nil forState:UIControlStateNormal];         
}];

[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
     [request startSynchronous];
}];                

All of the image names are unique. And they are not changing frequently. What I need is, keeping the image in the cache also after application finishes. And when I open the application again, it reads it from the downloaded cache.
Is it possible?

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Burak
  • 5,706
  • 20
  • 70
  • 110

1 Answers1

0

This is indeed possible. Your Cache Storage Policy is correct already.

You need to specify an appropiate Cache Policy as well, see http://allseeing-i.com/ASIHTTPRequest/How-to-use#about_cache_policies

The important thing here is to mind the data expiry date. In the default mode, ASIHTTPRequest will care for the expiry date, and if the data is expired, will reload the data. From my experience, often the server does not specify an appropiate expiry-date, so consider using ASIOnlyLoadIfNotCachedCachePolicy.

You can also specify the expiry date yourself if you want to (this might be even more interesting to you, because your images do change sometimes). Follow my link, all the information you need is there (see "Other cache-related features").

Oh and another thing: ASIHTTPRequest is not up-to-date anymore. The developer has abandoned the project. Therefore ASIHTTPRequest does not support ARC etc. If you can, switch to another framework like AFNetworking or RestKit.

JiaYow
  • 5,207
  • 3
  • 32
  • 36
  • Hi again. I moved to AFNetworking. Now, what should I do to load all the mages in the starting of thee application to the cache, and then use them inside the app from the cache? – Burak Apr 09 '12 at 13:07
  • You can use NSURLCache (it works together with UIImageView(AFNetworking) Category), or manually pull a persistent cache module (see discussion here:http://stackoverflow.com/questions/7529258/what-major-asihttprequest-features-is-afnetworking-missing). If you have problems using caching with AFNetworking/NSURLCache, search for it on SO or open another - properly tagged - question – JiaYow Apr 09 '12 at 15:00