1.新建project,拖出两个imageview和两个button,分别关联代码
2.button action中加入
- (IBAction)save:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic.png"]]; // 保存文件的名称
UIImage *image1 = _myimagview1.image;
BOOL result=[UIImagePNGRepresentation(image1)writeToFile: filePath atomically:YES];
if (result) {
NSLog(@"success");
}
}
- (IBAction)read:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic.png"]]; // 保存文件的名称
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
_myimageview2.image = img;
}
3.测试