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

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

4

1 回答 1

2

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

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

  • 在非方形图像中,确定缩略图大小的算法明显发生了变化。例如,对于我的示例 3500×2335 像素图像,当我创建一个 100 像素的缩略图时,它在 iOS 12.2 中生成了一个 100×67 像素的图像,但在 iOS 11.0.1 中是 100×66 像素。

  • 在方形图像中,两个 iOS 版本都生成了合适的方形缩略图。关于图像本身,我用肉眼看不到任何可观察到的差异。事实上,我把它放到 Photoshop 中并分析了差异(黑色 == 没有差异),它首先似乎表明没有任何变化:

    在此处输入图像描述

    只有当我开始真正进行像素窥视时,我才能检测到非常温和的变化。各个通道的差异很少超过 1 或 2(在这些UInt8值中)。这是相同的 delta 图像,这一次水平被放大了,因此您可以看到差异:

    在此处输入图像描述

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


在不相关的观察中,您的代码有一些泄漏。如果 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;
}
于 2019-03-22T17:48:54.740 回答