10

I'm running into a weird issue with my UIImage manipulation.

I'm doing a dropbox sync, and have to store my images as local files. To do so, I save them using the UIImagePNGRepresentation(image) or UIImageJPEGRepresentation(image, 0.66)

Here's my typical workflow: User selects an image or takes a photo Image gets assigned to an imageView The image view's UIImage gets saved to a file using the method below

Next time the user re-opens the app, I read the image back from the file using

[UIImage imageWithContentsOfFile:fullImagePath]

This works once.

The second time I run through the routine, I get the error message listed below when trying to save the image to disk once again.

//saving the image
NSData* data = isPNG?UIImagePNGRepresentation(image):UIImageJPEGRepresentation(image, 0.66);
BOOL success =   [data writeToFile:filepath atomically:YES];
if(!success)
{
    DLog(@"failed to write to file: %@",filepath );
}


//loading the image
[UIImage imageWithContentsOfFile:fullImagePath]

I'm getting this error when I try to re-save an image that has been previously converted to JPEG or PNG

2012-05-21 08:16:06.680   file already exists: NO
ImageIO: CGImageRead_mapData 'open' failed '/var/mobile/Applications/C1312BF8-C648-4397-82F3-D93E4FAAD35F/Documents/imageData/LogoImage.jpg'
error = 2 (No such file or directory)
May 21 08:16:06  <Error>: ImageIO: JPEG Not a JPEG file: starts with 0xff 0xd9
May 21 08:16:06  <Error>: ImageIO: JPEG Application transferred too few scanlines

It appears to me that this error is happening due to me trying to re-save the image as JPEG once again. If I ask the view to render itself in context (effectively creating a new image), the error does not happen.

Does anyone know what I'm doing wrong? Am I missing some kind of metadata by trying to convert a UIImage to data, reloading it, and trying to convert it once again after displaying it to the user?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • i think the problem might you are saving data with jpeg format and accessing png format ie once jpeg image data saved then when imageview accessing nsdata of png representation problem might be there – Paresh Navadiya May 21 '12 at 12:42
  • Very similar to: http://stackoverflow.com/questions/5081297/ios-uiimagejpegrepresentation-error-not-a-jpeg-file-starts-with-0xff-0xd9 – Francois Robert Nov 20 '12 at 20:35

1 Answers1

36

I've run into something similar before. It looked like imageWithContentsOfFile kept the file open as long as the UIImage it was used to create was still alive. At the time, I had gotten around the problem by reading the file into a NSData and then creating the UIImage from the NSData instead of the file directly.

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • 10
    You and your children's children shall be awesome for eternity! This happened to me with the open library of HJCache, using HJManagedImageV. I'm writing it, so search engines pick up on the connection. Thanks! – Oded Ben Dov Jun 25 '12 at 05:43
  • Holy crap, you just saved me several hours of work. Owe you one. – jpm Jul 25 '16 at 15:16
  • 1
    Thank you so much!!! I have spent the best part of a day, trying to get NSFileManager to save/load one damn image!!! Finally with your answer, it works, thanks again :) – Supertecnoboff Jul 29 '16 at 22:50
  • 3
    May the lord bless your soul. – Craig Aug 16 '18 at 16:44
  • Wish I could upvote more than once! Spent a day trying to figure this out. Your idea worked. Thanks so much. – Mike Taverne Jun 04 '19 at 17:32