2016-04-26 30 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]; 
相關問題