我有一个使用设备相机捕获的UIImage,捕获图像后,我像这样保存UIImage,
-(void)onCapture:(UIImage *)image {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *filemanager = [NSFileManager defaultManager];
NSString *pathToImage = [NSTemporaryDirectory() stringByAppendingPathComponent:@"my_photo_001.jpg"];
[filemanager createFileAtPath:pathToImage contents:imageData attributes:nil];
NSURL *imageFileUrl = [NSURL fileURLWithPath:pathToImage];
[self.delegate onDone:imageFileUrl];
}
将 NSURL 传递给委托后,委托将此 NSURL 传递给图像编辑视图控制器,我正在将 UIImage 以及 NSURL 传递给编辑视图控制器,一旦有人完成编辑,我想用编辑后的 UIImage 覆盖存储在 NSURL 的 UIImage,所以我正在这样做,
NSData *imageData = UIImageJPEGRepresentation(self.editImage.image, 1.0);
NSString *pathToImage = [pathOfCapturedImage absoluteString];
[imageData writeToFile:pathToImage atomically:YES];
NSURL *imageFileUrl = [NSURL URLWithString:pathToImage];
其中pathOfCapturedImage是我传递给编辑视图控制器的变量。
但是,当我在保存后打开图像时,它会打开未编辑的图像,我哪里出错了?
您可以先尝试删除旧文件,然后写入新文件;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
首先删除旧的存在图像,然后在同一路径上写入。
NSString* imagePath = @"image path"; // get path of old image.
NSData *imgData = UIImageJPEGRepresentation(self.editImage.image, 1.0); // get data of edited image. please make sure if edited image is not nil.
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) // check existence of old image, If yes then delete it.
{
[[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
}
if (imgData)
[imgData writeToFile:imagePath atomically:YES];