Created
May 28, 2014 02:13
-
-
Save Kjuly/4c46afab6171e16105ea to your computer and use it in GitHub Desktop.
A simple function from Nolan O'Brien that can be used to determine the type of image data based on the first couple bytes of the header.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static inline NSPUIImageType NSPUIImageTypeFromData(NSData *imageData) | |
{ | |
if (imageData.length > 4) { | |
const unsigned char * bytes = [imageData bytes]; | |
if (bytes[0] == 0xff && | |
bytes[1] == 0xd8 && | |
bytes[2] == 0xff) | |
{ | |
return NSPUIImageType_JPEG; | |
} | |
if (bytes[0] == 0x89 && | |
bytes[1] == 0x50 && | |
bytes[2] == 0x4e && | |
bytes[3] == 0x47) | |
{ | |
return NSPUIImageType_PNG; | |
} | |
} | |
return NSPUIImageType_Unknown; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Refer: http://nshipster.com/new-years-2014/