iOS UIImageJPEGRepresentation 错误:不是 JPEG 文件:以 0xff 0xd9 开头

发布于 2024-10-18 09:12:17 字数 703 浏览 6 评论 0原文

我正在将 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-10-25 09:12:17

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

我能够通过更改为消除错误

UIImage *myImage = [UIImage imageWithContentsOfFile:path];

消息

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

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];

to

NSData *img = [NSData dataWithContentsOfFile:path];
UIImage *photo = [UIImage imageWithData:img];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文