Detect if PNG file is corrupted in Objective C

后端 未结 3 2079
渐次进展
渐次进展 2021-01-03 06:43

I\'m downloading jpgs and pngs using NSURLRequest. This works ok but sometimes the files are corrupted. I have seen Catching error: Corrupt JPEG data: premature end of data

3条回答
  •  情书的邮戳
    2021-01-03 07:03

    Just as in Catching error: Corrupt JPEG data: premature end of data segment here is code snippet for PNG:

    - (BOOL)dataIsValidPNG:(NSData *)data
    {
        if (!data || data.length < 12)
        {
            return NO;
        }
    
        NSInteger totalBytes = data.length;
        const char *bytes = (const char *)[data bytes];
    
        return (bytes[0] == (char)0x89 && // PNG
                bytes[1] == (char)0x50 &&
                bytes[2] == (char)0x4e &&
                bytes[3] == (char)0x47 &&
                bytes[4] == (char)0x0d &&
                bytes[5] == (char)0x0a &&
                bytes[6] == (char)0x1a &&
                bytes[7] == (char)0x0a &&
    
                bytes[totalBytes - 12] == (char)0x00 && // IEND
                bytes[totalBytes - 11] == (char)0x00 &&
                bytes[totalBytes - 10] == (char)0x00 &&
                bytes[totalBytes - 9] == (char)0x00 &&
                bytes[totalBytes - 8] == (char)0x49 &&
                bytes[totalBytes - 7] == (char)0x45 &&
                bytes[totalBytes - 6] == (char)0x4e &&
                bytes[totalBytes - 5] == (char)0x44 &&
                bytes[totalBytes - 4] == (char)0xae &&
                bytes[totalBytes - 3] == (char)0x42 &&
                bytes[totalBytes - 2] == (char)0x60 &&
                bytes[totalBytes - 1] == (char)0x82);
    }
    

提交回复
热议问题