我通过 HTTP 下载图像时遇到此错误。我看过 answer here但即使是有效图像也不会从函数返回 YES。
还有其他想法吗?
获取图片的代码很简单。这发生在后台线程中。
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;
}
最佳答案
使用无符号字符。然后比较应该起作用。
const unsigned char * bytes = (const unsigned char *)[jpeg bytes];
代替
const char * bytes = (const char *)[jpeg bytes];
关于iphone - 图片IO : <ERROR> JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9265343/