Ios 使用CGImageDestinationCopyImageSource时,CGImageDestinationFinalize失败

Ios 使用CGImageDestinationCopyImageSource时,CGImageDestinationFinalize失败,ios,core-foundation,Ios,Core Foundation,我试图复制uiimage并更改元数据,而不重新编码该映像,以避免造成jpg瑕疵(我知道reencodes下面的代码,但这只是为了让它可以运行以进行测试)。CGImageDestinationCopyImageSource应该在不重新编码的情况下复制图像源,但当我使用该函数时,它在CGImageDestinationFinalize时失败。我正试图遵循这个技术说明 你知道为什么CGImageDestinationFinalize会因为下面的代码而失败吗 -(NSData *)copyImageAn

我试图复制uiimage并更改元数据,而不重新编码该映像,以避免造成jpg瑕疵(我知道reencodes下面的代码,但这只是为了让它可以运行以进行测试)。CGImageDestinationCopyImageSource应该在不重新编码的情况下复制图像源,但当我使用该函数时,它在CGImageDestinationFinalize时失败。我正试图遵循这个技术说明 你知道为什么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;
}

根据苹果公司的HeaderDoc文档,文件
中的
cImageDestinationCopyImageSource()
函数:

之后不应调用CGImageDestinationFinalize()-此函数返回时,结果将保存到目标


从各方面考虑,这是一个非常奇怪的API。