繁体   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);
                }
            }
        }
    }
}

在iOS12中为同一张图片获取不同的thumbnailData.length值。 我正在尝试使用CGImageSourceCreateThumbnailAtIndex()创建缩略图图像,并将sourceImage作为参数传递。 是iOS12错误吗? 是否有解决方法? 我正在使用iOS12 beta4。

的数据大小不同的,但所得到的图像是精细。 他们显然已经对该算法进行了一些非常适度的更改。 但是这里没有错误。

我个人注意到两个变化:

  • 在非正方形图像中,确定缩略图大小的算法已明显改变。 例如,对于我的示例3500×2335px图像,当我创建100px缩略图时,它在iOS 12.2中产生了100×67px图像,但在iOS 11.0.1中为100×66px。

  • 在正方形图像中,两个iOS版本均生成适当的正方形缩略图。 关于图像本身,我用肉眼看不到任何明显的区别。 实际上,我将其放到Photoshop中并分析了差异(黑色==无差异),它最初似乎暗示根本没有变化:

    在此处输入图片说明

    只有当我开始真正地窥视像素时,我才能检测到非常适度的变化。 各个通道之间的差异很少超过1或2(在这些UInt8值中)。 这是同一张Delta图像,这次的电平被炸毁,因此您可以看到差异:

    在此处输入图片说明

最重要的是,算法显然有一些变化,但是我不会将其描述为错误。 只是有所不同,但是效果很好。


在不相关的观察中,您的代码有一些泄漏。 如果Core Foundation方法的名称中包含“ Create或“ Copy ”,则您有责任将其释放(或以桥接类型将所有权转让给ARC,此处不提供此选项)。 静态分析器shift + Command + B非常适合识别这些问题。

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