如何识别 NSData 的图像格式?

发布于 2024-12-02 15:22:18 字数 84 浏览 1 评论 0原文

如果我得到一个 NSData,我知道它是图像的数据。但我不知道它是什么格式。 那么如何识别图片格式是Jpeg还是PNG呢?

PS:iOS

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离不开的别离 2024-12-09 15:22:18

我使用 Mats answer 在 NSData 上构建了一个简单的类别,它根据前 4 个字节告诉我其内容是 JPEG 还是 PNG:

@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

然后,只需执行以下操作:

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

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);
相思碎 2024-12-09 15:22:18

您可以查看第一个字节并进行猜测。互联网上有许多幻数列表,例如 http: //www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html

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.

一世旳自豪 2024-12-09 15:22:18

这是 @aouche 答案的 Swift 版本:

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

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
  }
}
金橙橙 2024-12-09 15:22:18

您可以从中创建一个图像,然后询问 NSImage 它是什么格式吗?

您可以使用 -initWithData 创建 NSImage,有关更多信息,请参阅 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html

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

方觉久 2024-12-09 15:22:18

您可以创建 CGImageSourceRef 然后询问其图像类型

    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;

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;
风流物 2024-12-09 15:22:18

如果你使用UIImage imageWithData,你不需要知道数据是png还是jpg。

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文