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

问题描述 投票:1回答:1
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。

ios objective-c ios12
1个回答
1
投票

数据大小不同,但生成的图像很好。他们显然对算法进行了一些非常适度的改动。但这里没有错误。

就个人而言,我注意到两个变化:

  • 在非方形图像中,用于确定缩略图大小的算法明显改变。例如,使用我的样本3500×2335px图像,当我创建100px缩略图时,它在iOS 12.2中产生100×67px图像,但在iOS 11.0.1中为100×66px。
  • 在方形图像中,两个iOS版本都生成了适当的方形缩略图。关于图像本身,我无法用肉眼看到任何可观察到的差异。事实上,我把它放到Photoshop中并分析了差异(黑色==没有区别),它首先似乎没有任何改变: enter image description here 只有当我开始真正像素窥视时才能检测到非常温和的变化。各个渠道很少差异超过1或2(在这些UInt8值)。这是相同的delta图像,这次是水平爆炸,所以你可以看到差异: enter image description here

最重要的是,算法显然有一些变化,但我不认为它是一个错误。它只是不同,但它工作正常。


在一个不相关的观察中,您的代码有一些泄漏。如果Core Foundation方法在名称中包含CreateCopy,则您负责释放它(或者,在桥接类型中,将所有权转移到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;
}
© www.soinside.com 2019 - 2024. All rights reserved.