Catching error: Corrupt JPEG data: premature end of data segment

后端 未结 2 693
你的背包
你的背包 2020-12-01 04:16

When creating an UIImage with corrupt/incomplete JPEG data, the console will print out

: Corrupt JPEG data: premature end of data segment<

2条回答
  •  星月不相逢
    2020-12-01 04:28

    In response to Slee's question above, this is the method I use:

    -(BOOL)dataIsValidJPEG:(NSData *)data
    {
        if (!data || data.length < 2) return NO;
    
        NSInteger totalBytes = data.length;
        const char *bytes = (const char*)[data bytes];
    
        return (bytes[0] == (char)0xff && 
                bytes[1] == (char)0xd8 &&
                bytes[totalBytes-2] == (char)0xff &&
                bytes[totalBytes-1] == (char)0xd9);
    }
    

提交回复
热议问题