从UIImageJPEGRepresentation()获取的iPhone iOS保存数据第二次失败:ImageIO:CGImageRead_mapData'open'失败 [英] iPhone iOS saving data obtained from UIImageJPEGRepresentation() fails second time: ImageIO: CGImageRead_mapData 'open' failed

查看:219
本文介绍了从UIImageJPEGRepresentation()获取的iPhone iOS保存数据第二次失败:ImageIO:CGImageRead_mapData'open'失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UIImage操作遇到了一个奇怪的问题。

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

我正在进行Dropbox同步,并且必须将我的图像存储为本地文件。为此,我使用 UIImagePNGRepresentation(图像) UIImageJPEGRepresentation(图片,0.66)保存它们

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)

这是我的典型工作流程:
用户选择图像或拍照
图像被分配到imageView
图像视图的UIImage被保存到使用以下方法的文件

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]

这是有效的一旦。

第二次运行例程时,我在尝试再次将映像保存到磁盘时收到下面列出的错误消息。

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]

当我尝试重新保存之前已转换为JPEG或PNG的图像时出现此错误

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

在我看来,由于我试图再次将图像重新保存为JPEG,因此发生此错误。如果我要求视图在上下文中呈现自己(有效地创建新图像),则不会发生错误。

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.

有谁知道我做错了什么?我是否因为尝试将UIImage转换为数据,重新加载以及在将其显示给用户后再次将其转换后错过某种元数据?

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?

推荐答案

我之前遇到过类似的事情。看起来 imageWithContentsOfFile 保持文件打开,只要它用于创建的 UIImage 仍然存在。当时,我通过将文件读入 NSData 然后从中创建 UIImage 来解决问题。 NSData 而不是直接文件。

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.

这篇关于从UIImageJPEGRepresentation()获取的iPhone iOS保存数据第二次失败:ImageIO:CGImageRead_mapData'open'失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆