Jimmy
Jimmy

Reputation: 1104

How to identify a NSData's image format?

If I get a NSData which I know it's a image's data.But I don't know what format it is. So how can I identify which image format is it?Jpeg or PNG?

PS:iOS

Upvotes: 9

Views: 3238

Answers (6)

James Grote
James Grote

Reputation: 53

If you use UIImage imageWithData, you don't need to know if the data is png or jpg.

// read from file
NSData * thumbnailData = [decoder decodeObjectForKey:kThumbnailKey];     
[UIImage imageWithData:thumbnailData];

Upvotes: 0

Viktor Goltvyanitsa
Viktor Goltvyanitsa

Reputation: 177

You can create CGImageSourceRef and then ask it for image type

    CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL);

    if(imageSource)
    {
        // this is the type of image (e.g., public.jpeg - kUTTypeJPEG )
        // <MobileCoreServices/UTCoreTypes.h>

        CFStringRef UTI = CGImageSourceGetType(imageSource);

        CFRelease(imageSource);
    }

    imageSource = nil;

Upvotes: 0

esad
esad

Reputation: 2700

Here's a Swift version of the @apouche's answer:

extension NSData {
  func firstBytes(length: Int) -> [UInt8] {
    var bytes: [UInt8] = [UInt8](count: length, repeatedValue: 0)
    self.getBytes(&bytes, length: length)
    return bytes
  }

  var isJPEG: Bool {
    let signature:[UInt8] = [0xff, 0xd8, 0xff, 0xe0]
    return firstBytes(4) == signature
  }

  var isPNG: Bool {
    let signature:[UInt8] = [0x89, 0x50, 0x4e, 0x47]
    return firstBytes(4) == signature
  }
}

Upvotes: 1

apouche
apouche

Reputation: 9983

I used Mats answer to build a simple category on NSData which tells me if its content is JPEG or PNG based on its first 4 bytes:

@interface NSData (yourCategory)

- (BOOL)isJPG;
- (BOOL)isPNG;

@end

@implementation NSData (yourCategory)
- (BOOL)isJPG
{
    if (self.length > 4)
    {
        unsigned char buffer[4];
        [self getBytes:&buffer length:4];

        return buffer[0]==0xff && 
               buffer[1]==0xd8 && 
               buffer[2]==0xff &&
               buffer[3]==0xe0;
    }

    return NO;
}

- (BOOL)isPNG
{
    if (self.length > 4)
    {
        unsigned char buffer[4];
        [self getBytes:&buffer length:4];

        return buffer[0]==0x89 &&
               buffer[1]==0x50 &&
               buffer[2]==0x4e &&
               buffer[3]==0x47;
    }

    return NO;
}

@end

And then, simply do a :

CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef) imgData);
CGImageRef imgRef = nil;

if ([imgData isJPG])
    imgRef = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
else if ([imgData isPNG])
    imgRef = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);

UIImage* image = [UIImage imageWithCGImage:imgRef];

CGImageRelease(imgRef);
CGDataProviderRelease(imgDataProvider);

Upvotes: 17

Mats
Mats

Reputation: 8638

You could look at the first bytes and make a guess. There are many lists of magic numbers available on the internet, e.g. http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html.

Upvotes: 8

Ibolit
Ibolit

Reputation: 9720

Can you create an image from that and then just ask that NSImage what format it is?

You can use -initWithData to create the NSImage, for more, see http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html

Upvotes: 0

Related Questions