【问题标题】:CGImageSourceCreateThumbnailAtIndex() giving different results in iOS11 and iOS12CGImageSource CreateThumbnailAtIndex() 在 iOS 11 和 iOS 12 中给出不同的结果
【发布时间】:2019-03-23 01:57:34
【问题描述】:
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】:

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

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

    • 在非方形图像中,确定缩略图大小的算法明显发生了变化。例如,对于我的示例 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多