ImageIO:<错误>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];
这是那个线程的函数:
- (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];