How to determine if an image is a progressive JPEG (interlaced) in Objective-C

百般思念 提交于 2020-01-03 04:55:23

问题


I need to determine in Objective-C if an image, that needs to be downloaded and shown, is a progressive JPEG. I did some search but didn't find any obvious way. Can you help?


回答1:


The JPEG Format contain several markers.

The Progressive Markers are

\xFF\xC2
\xFF\xDA

If you find these in the file, then you're dealing with progressive JPEGs.




回答2:


After doing some more research I found some more info on the way it could be done in iOS.

Following what I read in Technical Q&A QA1654 - Accessing image properties with ImageIO I did it like this:

//Example of progressive JPEG
NSString *imageUrlStr = @"http://cetus.sakura.ne.jp/softlab/software/spibench/pic_22p.jpg";

CFURLRef url = (__bridge CFURLRef)[NSURL URLWithString:imageUrlStr];

CGImageSourceRef myImageSource = CGImageSourceCreateWithURL(url, NULL);

CFDictionaryRef imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(myImageSource,0, NULL);

The imagePropertiesDictionary dictionary looks like this for a progressive JPEG:

Printing description of imagePropertiesDictionary:
{
    ColorModel = RGB;
    Depth = 8;
    PixelHeight = 1280;
    PixelWidth = 1024;
    "{JFIF}" =     {
        DensityUnit = 0;
        IsProgressive = 1;
        JFIFVersion =         (
            1,
            0,
            1
        );
        XDensity = 1;
        YDensity = 1;
    };
}

IsProgressive = 1 means it's a progressive/interlaced JPEG. You can check the value like this:

CFDictionaryRef JFIFDictionary = (CFDictionaryRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyJFIFDictionary);

CFBooleanRef isProgressive = (CFBooleanRef)CFDictionaryGetValue(JFIFDictionary, kCGImagePropertyJFIFIsProgressive);

if(isProgressive == kCFBooleanTrue)
    NSLog(@"It's a progressive JPEG");


来源:https://stackoverflow.com/questions/33154022/how-to-determine-if-an-image-is-a-progressive-jpeg-interlaced-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!