How to store images in cache?

前端 未结 2 822
南旧
南旧 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.

提交回复
热议问题