How to get errors/warnings in [UIImage initWithData:]

拥有回忆 提交于 2019-12-01 22:43:53
Alex Nichol

There is a similar thread to this one on stack overflow: Catching error: Corrupt JPEG data: premature end of data segment.

There solution is to check for the header bytes FF D8 and ending bytes FF D9. So, if you have image data in an NSData, you can check it like so:

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

Then, to check if JPEG data is invalid, just write:

if (![self isJPEGValid:myData]) {
    NSLog(@"Do something here");
}

Hope this helps!

The initWithData: method should return nil in such cases.

Try :

UIImage *myImage = [[UIImage alloc] initWithData:imgData];
if(!myImage) {
    // problem
}

I've encountered the same problem in this exact situation.

It turned out that I was passing an instance of NSMutableData to global queue for decoding. During decoding the data in NSMutableData was overwritten by next frame received from network.

I've fixed the errors by passing a copy of the data. It might be better to use a buffer pool to improve performance:

    NSData *dataCopy = [_receivedData copy];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    ZAssert([self isJPEGValid:dataCopy], @"JPEG data is invalid"); // should never happen
    UIImage *image = [UIImage imageWithData:dataCopy];
    dispatch_async(dispatch_get_main_queue(), ^{
        // show image
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!