简体   繁体   English

CGImageSourceCreateThumbnailAtIndex()在iOS11和iOS12中给出不同的结果

[英]CGImageSourceCreateThumbnailAtIndex() giving different results in iOS11 and iOS12

CGImageRef        thumbnailImage = NULL;
CGImageSourceRef  imageSource = NULL;
CFDictionaryRef   createOptions = NULL;
CFStringRef       createKeys[3];
CFTypeRef         createValues[3];
CFNumberRef       thumbnailSize = 0;
UIImage * thumbnail;
NSData * squareData = UIImagePNGRepresentation(sourceImage);
NSData * thumbnailData = nil;

imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)squareData,NULL);
if (imageSource)
{
    thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);
    if (thumbnailSize)
    {
        createKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
        createValues[0] = (CFTypeRef)kCFBooleanTrue;
        createKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
        createValues[1] = (CFTypeRef)kCFBooleanTrue;
        createKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
        createValues[2] = (CFTypeRef)thumbnailSize;

        createOptions = CFDictionaryCreate(NULL, (const void **) createKeys,
                createValues, sizeof(createValues)/ sizeof(createValues[0]),
                &kCFTypeDictionaryKeyCallBacks,
                & kCFTypeDictionaryValueCallBacks);
        if (createOptions)
        {
            thumbnailImage = CGImageSourceCreateThumbnailAtIndex(imageSource,0,createOptions);
            if(thumbnailImage)
            {
                thumbnail = [UIImage imageWithCGImage:thumbnailImage];
                if (thumbnail)
                {
                    thumbnailData = UIImagePNGRepresentation(thumbnail);
                }
            }
        }
    }
}

Getting different thumbnailData.length value for the same image in iOS12. 在iOS12中为同一张图片获取不同的thumbnailData.length值。 I am trying to create a thumbnail image using CGImageSourceCreateThumbnailAtIndex() and passing sourceImage as a parameter. 我正在尝试使用CGImageSourceCreateThumbnailAtIndex()创建缩略图图像,并将sourceImage作为参数传递。 Is it an iOS12 bug? 是iOS12错误吗? Is there a workaround for it? 是否有解决方法? I'm using iOS12 beta4. 我正在使用iOS12 beta4。

The data size is different, but the resulting image is fine. 的数据大小不同的,但所得到的图像是精细。 They've clearly made some very modest changes to the algorithm. 他们显然已经对该算法进行了一些非常适度的更改。 But there is no bug here. 但是这里没有错误。

Personally, I notice two changes: 我个人注意到两个变化:

  • In non-square images, the algorithm for determining the size of the thumbnail has obviously changed. 在非正方形图像中,确定缩略图大小的算法已明显改变。 Eg, with my sample 3500×2335px image, when I created a 100px thumbnail, it resulted in a 100×67px image in iOS 12.2, but was 100×66px in iOS 11.0.1. 例如,对于我的示例3500×2335px图像,当我创建100px缩略图时,它在iOS 12.2中产生了100×67px图像,但在iOS 11.0.1中为100×66px。

  • In square images, the two iOS versions both generated suitably square thumbnails. 在正方形图像中,两个iOS版本均生成适当的正方形缩略图。 Regarding the image, itself, I could not see much of any observable difference with the naked eye. 关于图像本身,我用肉眼看不到任何明显的区别。 In fact, I dropped this into Photoshop and analyzed the differences (where black == no difference), it first seemed to suggest no change at all: 实际上,我将其放到Photoshop中并分析了差异(黑色==无差异),它最初似乎暗示根本没有变化:

    在此处输入图片说明

    Only when I started to really pixel-peep could I detect the very modest changes. 只有当我开始真正地窥视像素时,我才能检测到非常适度的变化。 The individual channels rarely differed by more than 1 or 2 (in these UInt8 values). 各个通道之间的差异很少超过1或2(在这些UInt8值中)。 Here is the same delta image, this time with the levels blown out so you can see the differences: 这是同一张Delta图像,这次的电平被炸毁,因此您可以看到差异:

    在此处输入图片说明

Bottom line, there clearly is some change to the algorithm, but I wouldn't characterize it as a bug. 最重要的是,算法显然有一些变化,但是我不会将其描述为错误。 It is just different, but it works fine. 只是有所不同,但是效果很好。


In an unrelated observation, your code has some leaks. 在不相关的观察中,您的代码有一些泄漏。 If a Core Foundation method has Create or Copy in the name, you are responsible for releasing it (or, in bridged types, transferring ownership to ARC, which isn't an option here). 如果Core Foundation方法的名称中包含“ Create或“ Copy ”,则您有责任将其释放(或以桥接类型将所有权转让给ARC,此处不提供此选项)。 The static analyzer, shift + command + B , is excellent at identifying these issues. 静态分析器shift + Command + B非常适合识别这些问题。

FWIW, here's my rendition: FWIW,这是我的陈述:

- (UIImage * _Nullable)resizedImage:(UIImage *)sourceImage to:(NSInteger)imageSize {
    NSData *squareData = UIImagePNGRepresentation(sourceImage);
    UIImage *thumbnail = nil;

    CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)squareData, NULL);
    if (imageSource) {
        NSDictionary *createOptions = @{
            (id)kCGImageSourceCreateThumbnailWithTransform: @true,
            (id)kCGImageSourceCreateThumbnailFromImageIfAbsent: @true,
            (id)kCGImageSourceThumbnailMaxPixelSize: @(imageSize)
        };
        CGImageRef thumbnailImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (CFDictionaryRef)createOptions);
        if (thumbnailImage) {
            thumbnail = [UIImage imageWithCGImage:thumbnailImage];
            if (thumbnail) {
                NSData *data = UIImagePNGRepresentation(thumbnail);
                // do something with `data` if you want
            }
            CFRelease(thumbnailImage);
        }
        CFRelease(imageSource);
    }

    return thumbnail;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM