iOS UIImageJPEGRepresentation 错误:不是 JPEG 文件:以 0xff 0xd9 开头
我正在将 .jpg 文件写入我的应用程序的 Documents 目录,如下所示:
NSData *img = UIImageJPEGRepresentation(myUIImage, 1.0);
BOOL retValue = [img writeToFile:myFilePath atomically:YES];
稍后,我使用以下方法将该图像加载回 UIImage:
UIImage *myImage = [UIImage imageWithContentsOfFile:path];
我知道它可以工作,因为我可以在表格单元格中绘制图像,而且效果很好。现在,如果我尝试使用 UIImageJPEGRepresentation(myImage, 1.0),调试器会打印出这些行:
<Error>: Not a JPEG file: starts with 0xff 0xd9
<Error>: Application transferred too few scanlines
并且该函数返回 nil。有人知道为什么会发生这种情况吗?加载 UIImage 数据后,我没有做任何操作。我刚刚将 UIImage 提供给单元格中的图像视图。我设置图像视图属性,使单元格中的所有图像对齐且大小相同,但我认为这与能够将 UIImage 转换为 NSData 没有任何关系。
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];
Later, I load that image back into a UIImage using:
UIImage *myImage = [UIImage imageWithContentsOfFile:path];
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
图像没有损坏,我能够正确显示它们。该问题可能是 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
to