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?
I’ve run into something similar before. It looked like
imageWithContentsOfFilekept the file open as long as theUIImageit was used to create was still alive. At the time, I had gotten around the problem by reading the file into aNSDataand then creating theUIImagefrom theNSDatainstead of the file directly.