iOS UIImageJPEGRepresentation错误:不是JPEG文件:以0xff 0xd9开头 [英] iOS UIImageJPEGRepresentation error: Not a JPEG file: starts with 0xff 0xd9

查看:1090
本文介绍了iOS UIImageJPEGRepresentation错误:不是JPEG文件:以0xff 0xd9开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将.jpg文件写入我的应用文档目录,如下所示:

I am writing a .jpg file to my app's Documents directory like this:

NSData *img = UIImageJPEGRepresentation(myUIImage, 1.0);
BOOL retValue = [img writeToFile:myFilePath atomically:YES];

稍后,我使用以下方法将该图像加载回UIImage:

Later, I load that image back into a UIImage using:

UIImage *myImage = [UIImage imageWithContentsOfFile:path];

我知道它有效,因为我可以在表格单元格中绘制图像,这很好。现在,如果我尝试使用UIImageJPEGRepresentation(myImage,1.0),调试器将打印出这些行:

I know it works because I can draw the image in a table cell and it is fine. Now if I try to use UIImageJPEGRepresentation(myImage, 1.0), the debugger prints out these lines:

<Error>: Not a JPEG file: starts with 0xff 0xd9
<Error>: Application transferred too few scanlines

并且函数返回nil。有人知道为什么会这样吗?加载后我没有做任何事情来操纵UIImage数据。我刚刚将UIImage提供给单元格中的图像视图。我设置了图像视图属性,使得单元格中的所有图像对齐并且大小相同,但我认为这与将UIImage转换为NSData无关。

And the function returns nil. Does anybody have an idea why this would happen? I haven't done anything to manipulate the UIImage data after it was loaded. I just provided the UIImage to an image view in a cell. I set the image view properties such that all the images in the cells line up and are the same size, but I don't think that should have anything to do with being able to convert the UIImage to NSData.

推荐答案

图像没有损坏,我能够正确显示它们。问题可能是UIImage中的一个错误,或者文档应该更清楚使用imageWithContentsOfFile:。

The images were not corrupt, I was able to display them correctly. The issue is possibly a bug in UIImage, or perhaps the documentation should be more clear about using imageWithContentsOfFile:.

我能够通过更改

I was able to eliminate the error message by changing

UIImage *myImage = [UIImage imageWithContentsOfFile:path];

NSData *img = [NSData dataWithContentsOfFile:path];
UIImage *photo = [UIImage imageWithData:img];

这篇关于iOS UIImageJPEGRepresentation错误:不是JPEG文件:以0xff 0xd9开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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