iPhone iOS saving data obtained from UIImageJPEGRepresentation() fails second time: ImageIO: CGImageRead_mapData 'open' failed

*爱你&永不变心* 提交于 2019-12-03 17:33:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10685276/iphone-ios-saving-data-obtained-from-uiimagejpegrepresentation-fails-second-ti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!