0

I have an UIImage that is getting captured using the device camera, After the image is captured I am saving the UIImage like this,

-(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];
}

After the NSURL is passed to the delegate the delegate then passes this NSURL to an image editing view controller, I am passing the UIImage as well as the NSURL to the editing view controller, Once someone is done editing I want to override the UIImage stored at the NSURL with the edited UIImage so I am doing this,

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

NSURL *imageFileUrl = [NSURL URLWithString:pathToImage];

Where pathOfCapturedImage is the variable I am passing to my edit view controller.

However when I am opening the image after saving it opens up the unedited image, Where am I going wrong?

Avinash Sharma
  • 665
  • 1
  • 7
  • 23
  • pl. check that edited image is stored correctly or not. – Mahendra Apr 26 '16 at 09:05
  • when you are saving edited image then print the path in console...after that copy that path -> Open finder -> Press command + Shift + G and paste path and press enter you will moved to image location – Mahendra Apr 26 '16 at 09:27
  • file:///private/var/mobile/Applications/28FCBDE0-2D4E-47BC-AA32-FB02A055EB49/tmp/my_photo_001.jpg I get this path when i copy this path and find it shows me cannot find folder? – Avinash Sharma Apr 26 '16 at 09:31
  • @AvinashDadhich You cannot access files like image directly from iPhone! – byJeevan Apr 26 '16 at 10:22
  • @avinash for that you need to download container from your iPhone and then check it out. – Mahendra Apr 26 '16 at 10:37

2 Answers2

0

You can try delete the old file first, and the write the new one;

[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
4oby
  • 607
  • 10
  • 19
0

First delete old exist image then write on the same path.

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];
tauheed
  • 161
  • 1
  • 12