How to store images in cache?

2020-03-30 05:31发布

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.

标签: iphone
2条回答
戒情不戒烟
2楼-- · 2020-03-30 06:01

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.

查看更多
干净又极端
3楼-- · 2020-03-30 06:13

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];
查看更多
登录 后发表回答