1

in iphone i have requirement of store image in cache .

but how i can store this in cache.

I know the folder path of cache. is this only way i have to WRITE my image in that folder for cache?

I want to know any other way. or is this correct way? Please help me.

2 Answers2

2

You can store data on local disk using NSArchiver and NSUnarchiver in this way:

// set your image in cache

NSData *imgData = [NSKeyedArchiver archivedDataWithRootObject:myImage];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:imgData forKey:@"img_01"];
[defaults synchronize];

//get your image from cache

NSData *imgData = [[NSUserDefaults standardUserDefaults] objectForKey:@"img_01"];
myImage = [NSKeyedUnarchiver unarchiveObjectWithData:imgData];

Reference apple here: http://goo.gl/XH2o2

hope this helps.

elp
  • 8,021
  • 7
  • 61
  • 120
  • 2
    -1 Explanation: NSUserDefaults is not a global storage. NSUserDefaults should be solely used for preferences. But because of the singleton design, people think NSUserDefaults is the successor of the AppDelegate-Superstorage. It's easy to use, available everywhere and "It just works", though. But It's very bad to do so. NSUserDefaults saves its content to a single plist file. And because of this the suggestion to build a cache (= more than one image, probably thousands) with NSUserDefaults as a backend is a bad answer. – Matthias Bauch Feb 15 '12 at 08:57
  • But if you'll do that kind of cache, just for a few images, it'll work so good. I use that in production, for a little time cache, and it works without any problem. – Abner Terribili Sep 21 '16 at 11:13
0

use this link..

http://www.makebetterthings.com/iphone/image-caching-in-iphone-sdk/

edited:
get file path using this code and fatch the particular file :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
kulss
  • 2,057
  • 1
  • 14
  • 16
  • i know this way. i asked that is this only way ? i cant do this by any other way ? –  Feb 15 '12 at 09:15
  • i want to here this thing.i like to know that. can you please tell how can i do that ? –  Feb 15 '12 at 10:42
  • 1
    get the file path useing this : NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; – kulss Feb 15 '12 at 10:51
  • ohh no no apple reject my application for this. they told in front you are talking store data in cache and you are using other library so i have to store in this library. but i am finding is this default way to store it ? if no then i will use WRITE way. –  Feb 15 '12 at 10:58
  • use temporary dir instead of LibraryDirectory , save temporary dir is the better way because as per the new guideline of apple, we should to save data in temporary dir is more acceptable use this: NSString *tmpDir = NSTemporaryDirectory(); NSString *tmpFilename = @"XXXXX"; NSString *tmpPath = [tmpDir stringByAppendingPathComponent:tmpFilename]; – kulss Feb 15 '12 at 11:15
  • thanks kulss .i think this is the final way. Let me use this only. –  Feb 17 '12 at 07:13