iOS开发-缓存图片到沙盒--思路

来源:互联网 发布:秦九韶算法 求导数 编辑:程序博客网 时间:2024/05/21 15:03

1.图片存储到沙盒中

?
1
2
3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%d.png", myI]];   // 保存文件的名称
        [UIImagePNGRepresentation(myImage)writeToFile: filePath    atomically:YES];

2.在plist中保存路径

?
1
2
3
4
5
6
7
8
NSMutableDictionary *info = [[NSMutableDictionary alloc]init];
 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%d.png", conut_]];   // 保存文件的名称
 
[info setObject:filePath forKey:@"img"];
[specialArr addObject:info];

3.使用图片

?
1
2
3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%d.png", (int)current]];  // 保存文件的名称
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
以上是OC语言,如果可以改成swift3.0语言更佳,以后有时间来更新一下。
0 0
原创粉丝点击