How to store images in cache?

前端 未结 2 781
南旧
南旧 2021-01-16 15:25

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

相关标签:
2条回答
  • 2021-01-16 16:10

    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.

    0 讨论(0)
  • 2021-01-16 16:16

    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];
    
    0 讨论(0)
提交回复
热议问题