0

我有一个使用设备相机捕获的 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带有已编辑 UIImage 的 NSURL 所以我正在这样做,

NSData *imageData = UIImageJPEGRepresentation(self.editImage.image, 1.0);
NSString *pathToImage = [pathOfCapturedImage absoluteString];
[imageData writeToFile:pathToImage atomically:YES];

NSURL *imageFileUrl = [NSURL URLWithString:pathToImage];

pathOfCapturedImage 是我传递给编辑视图控制器的变量。

但是,当我在保存后打开图像时会打开未编辑的图像,我哪里出错了?

4

2 回答 2

0

首先删除旧的存在图像,然后在同一路径上写入。

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];
于 2016-04-26T10:19:58.027 回答
0

您可以尝试先删除旧文件,然后写入新文件;

[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
于 2016-04-26T09:05:25.670 回答