gpt4 book ai didi

ios - CGImageSource CreateThumbnailAtIndex() 在 iOS 11 和 iOS 12 中给出不同的结果

转载 作者:行者123 更新时间:2023-12-01 19:43:49 25 4
gpt4 key购买 nike

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。

最佳答案

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

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

  • 在非方形图像中,确定缩略图大小的算法明显发生了变化。例如,对于我的示例 3500×2335 像素图像,当我创建一个 100 像素的缩略图时,它在 iOS 12.2 中生成了一个 100×67 像素的图像,但在 iOS 11.0.1 中是 100×66 像素。
  • 在方形图像中,两个 iOS 版本都生成了合适的方形缩略图。关于图像本身,我用肉眼看不到任何可观察到的差异。事实上,我把它放到 Photoshop 中并分析了差异(黑色 == 没有差异),它首先似乎表明没有任何变化:

    enter image description here

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

    enter image description here

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

    在不相关的观察中,您的代码有一些泄漏。如果核心基础方法具有 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;
    }

    关于ios - CGImageSource CreateThumbnailAtIndex() 在 iOS 11 和 iOS 12 中给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420633/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com