ImageIO: <ERROR> JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?

喜你入骨 提交于 2019-11-29 18:34:38

问题


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.

Any other ideas?

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

回答1:


Use an unsigned char. Then comparing should work.

const unsigned char * bytes = (const unsigned char *)[jpeg bytes];

instead of

const char * bytes = (const char *)[jpeg bytes];


来源:https://stackoverflow.com/questions/9265343/imageio-error-jpeg-corrupt-jpeg-data-premature-end-of-data-segment-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!