图片缓存思路

来源:互联网 发布:数据库组成与原理五版 编辑:程序博客网 时间:2024/05/17 23:17
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);        NSString *documentsDirectory = [paths objectAtIndex:0];            //生成plist文件    NSString *plistPath=[documentsDirectory stringByAppendingPathComponent:@"imageInfo.plist"];    NSFileManager* fm = [NSFileManager defaultManager];    [fm createFileAtPath:plistPath contents:nil attributes:nil];        //图片存储到沙盒中    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test.png"];   // 保存文件的路径    UIImage *myImage;//要存储的图片。。。    [UIImagePNGRepresentation(myImage) writeToFile: filePath  atomically:YES];            //在plist中保存路径    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];    [dic setObject:filePath forKey:@"myimage"];//保存图片存储的路径到plist中    [dic writeToFile:plistPath atomically:YES];            //使用图片    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:plistPath];  //读取数据    if (dic2 == nil) {        //1. 创建一个plist文件        NSFileManager* fm = [NSFileManager defaultManager];        [fm createFileAtPath:plistPath contents:nil attributes:nil];    }else{        UIImage *img = [UIImage imageWithContentsOfFile:[dic2 objectForKey:@"myimage"]];    }

1 0
原创粉丝点击