2016-04-26 38 views
0

我有,这是获得使用该设备摄像头拍摄的,图像捕捉我保存的UIImage这样后的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是我传递给我的编辑视图控制器的变量。

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

+0

pl。检查编辑的图像是否正确存储。 –

+0

我该如何检查? –

+0

当你保存编辑的图像,然后在控制台中打印路径...之后,复制该路径 - >打开查找器 - >按下命令+ Shift + G并粘贴路径,然后按回车你将移动到图像位置 –

回答

0

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

[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]; 
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]; 
相关问题