Skip to content

Instantly share code, notes, and snippets.

@pyanfield
Last active December 31, 2015 22:08
Show Gist options
  • Save pyanfield/8051131 to your computer and use it in GitHub Desktop.
Save pyanfield/8051131 to your computer and use it in GitHub Desktop.
check the image's type, jpeg or png
-(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);
}
- (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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment