ImageIO: lt;ERRORgt; JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?(ImageIO:lt;错误gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?)
问题描述
我通过 HTTP 下载图像时收到此错误.我查看了 此处的答案,但即使是有效图像不要从函数返回 YES
.
I get this error by downloading an image by HTTP. I have looked at the answer here but even the valid images don't return YES
from the function.
还有其他想法吗?
获取图像的代码很简单.这发生在后台线程中.
The code to get the image is simple enough. This happens in a background thread.
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *image = [UIImage imageWithData:data];
这是来自该线程的函数:
This is the function from that thread:
- (BOOL)isJPEGValid:(NSData *)jpeg {
if ([jpeg length] < 4) return NO;
const char * bytes = (const char *)[jpeg bytes];
if (bytes[0] != 0xFF || bytes[1] != 0xD8) return NO;
if (bytes[[jpeg length] - 2] != 0xFF ||
bytes[[jpeg length] - 1] != 0xD9) return NO;
return YES;
}
推荐答案
使用无符号字符.然后比较应该可以工作.
Use an unsigned char. Then comparing should work.
const unsigned char * bytes = (const unsigned char *)[jpeg bytes];
而不是
const char * bytes = (const char *)[jpeg bytes];
这篇关于ImageIO:<错误>JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ImageIO:<错误>JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?


基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01