开发者

How to identify a NSData's image format?

开发者 https://www.devze.com 2023-04-02 03:58 出处:网络
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 i开发者_开发技巧dentify which image format is it?Jpeg or PNG?

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 i开发者_开发技巧dentify which image format is it?Jpeg or PNG?

PS:iOS


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);


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.


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
  }
}


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


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;


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消