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

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

问题描述

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

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

我正在执行 Dropbox 同步,并且必须将我的图像存储为本地文件.为此,我使用 UIImagePNGRepresentation(image)UIImageJPEGRepresentation(image, 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 然后从 NSData 而不是文件创建 UIImage 解决了这个问题直接.

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.

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

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