将图片存取的路径设置在沙盒中 Document\ 目录下的
保存图片代码:
UIImage *image = [UIImage imageNamed:@"myImage"];
NSString * imageFileName = @"myImage";
//获取保存路径
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [documentDirectories firstObject];
NSString *path = [documentDirectory stringByAppendingPathComponent:imageFileName];
//从图片提取JEPG格式的数据,0.5表示压缩质量,范围在0-1之间,1代表最高质量(不压缩)
NSData *data = UIImageJPEGRepresentation(image, 0.5);
//从图片提取PNG格式的
//NSData *data = UIImagePNGRepresentation(image);
//将JPEG格式的数据写入文件
[data writeToFile:path atomically:YES];
在 Document\ 目录下就会生成一个叫 ‘myImage’ 的文件,打开这个文件,能看到想要保存的图片
获取图片代码:
NSString * imageFileName = @"myImage";
//获取路径
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [documentDirectories firstObject];
NSString *path = [documentDirectory stringByAppendingPathComponent:imageFileName];
UIImage *image = [UIImage imageWithContentsOfFile:path];
删除图片文件代码:
NSString * imageFileName = @"myImage";
//获取路径
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [documentDirectories firstObject];
NSString *path = [documentDirectory stringByAppendingPathComponent:imageFileName];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];