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
Nicer version of dataIsValidPNG:
BOOL dataIsValidPNG(NSData *data) {
if (!data) {
return NO;
}
const NSInteger totalBytes = data.length;
const char *bytes = (const char *)[data bytes];
const char start[] = { '\x89', 'P', 'N', 'G', '\r', '\n', '\x1a', '\n' };
const char end[] = { '\0', '\0', '\0', '\0', 'I', 'E', 'N', 'D', '\xAE', 'B', '`', '\x82' };
if (totalBytes < (sizeof(start) + sizeof(end))) {
return NO;
}
return (memcmp(bytes, start, sizeof(start)) == 0) &&
(memcmp(bytes + (totalBytes - sizeof(end)), end, sizeof(end)) == 0);
}