ios - 使用 CGImageDestinationCopyImageSource 时 CGImageDestinationFinalize 失败

标签 ios core-foundation

我正在尝试复制 uiimage 并更改元数据,而不重新编码图像,以免导致 jpg 伪影(我知道下面的代码重新编码,但这只是为了可以运行它进行测试)。 CGImageDestinationCopyImageSource 应该复制图像的源而不重新编码它,但是当我使用该函数时,它在 CGImageDestinationFinalize 处失败。我正在尝试遵循此技术说明 https://developer.apple.com/library/content/qa/qa1895/_index.html 知道为什么 CGImageDestinationFinalize 会因下面的代码而失败吗?

-(NSData *)copyImageAndChangeMetaData:(UIImage *)image {

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
    NSLog(@"    source: 0x%x", source);
    if (source == NULL) {
        NSLog(@"    source is NULL");
    }

    CFStringRef UTI = CGImageSourceGetType(source);
    NSLog(@"    UTI: %@", (__bridge NSString *)UTI);
    if (UTI == NULL) {
        NSLog(@"    UTI is NULL");
    }

    NSMutableData *dest_data = [NSMutableData data];
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1, NULL);
    if (!destination)
    {
         NSLog(@"Image Data Error: Could not create image destination");
    }

    CFMutableDictionaryRef metadata = CFDictionaryCreateMutable(kCFAllocatorDefault, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    int orient = 8;
    CFNumberRef orientRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &orient);
    CFDictionarySetValue(metadata, kCGImageDestinationOrientation, orientRef);

    CFStringRef dateStringRef = CFStringCreateWithCString(kCFAllocatorDefault, "2017-06-06T17:31:46Z", kCFStringEncodingASCII);
    CFDictionarySetValue(metadata, kCGImageDestinationDateTime, dateStringRef);

    CFErrorRef errorRef = nil;
    if (!CGImageDestinationCopyImageSource(destination, source, metadata, &errorRef)) {
        CFStringRef error_description = CFErrorCopyDescription(errorRef);

        NSString *errorDescription = (__bridge NSString *)error_description;
        NSLog(@"Image Data Error: Error copying image data: %@", errorDescription);

        CFRelease(error_description);
    }

    BOOL success = NO;
    success = CGImageDestinationFinalize(destination);
    if (!success)
    {
        NSLog(@"Image Data Error: Could not finalize image");
    }
    if (source != NULL) {
        CFRelease(source);
    }

    if (destination != NULL) {
        CFRelease(destination);
    }

    return dest_data;
}

最佳答案

根据 Apple 的 HeaderDoc 文档 CGImageDestinationCopyImageSource()文件中的函数<ImageIO/CGImageDestination.h> :

CGImageDestinationFinalize() should not be called afterward - the result is saved to the destination when this function returns.

关于ios - 使用 CGImageDestinationCopyImageSource 时 CGImageDestinationFinalize 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400559/

相关文章:

ios - 以编程方式将自定义 UIViewcontroller 添加到 subview 但收到错误消息 "Cannot convert value of type..."

ios - 检测应用程序何时关闭、终止但在手机关闭时忽略

ios - 创建自定义核心基础类型。

c++ - CoreFoundation 打印 Unicode 字符

ios - OS X/iOS 和 TCP : Why CFSocketSendData crashes application if called after connection is reset?

objective-c - NSCharacterSet - 添加另一个字符集

ios - 表格 View 单元格可扩展 iOS

ios - TableView 重新加载过早

iphone - NSNumberFormatter 添加括号

ios - 将对齐数组 uint8[8] 转换为 double