CGImageDestinationFinalize使用CGImageDestinationCopyImageSource失败
问题描述:
我想复制一个uiimage并更改元数据,而不用重新编码图像,以便不会导致jpg工件(我知道下面的代码重新编码,但这只是为了它可以运行测试)。 CGImageDestinationCopyImageSource应该复制图像的来源而不用重新编码它,但是当我使用该函数时,它在CGImageDestinationFinalize时失败。我试图按照这个技术说明https://developer.apple.com/library/content/qa/qa1895/_index.html 任何想法为什么CGImageDestinationFinalize会失败,下面的代码?CGImageDestinationFinalize使用CGImageDestinationCopyImageSource失败
-(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;
}
答
根据文件<ImageIO/CGImageDestination.h>
苹果HeaderDoc文档CGImageDestinationCopyImageSource()
功能:)
CGImageDestinationFinalize(不应该被称为后 - 结果保存到目的地时,该函数返回。
这是一个非常奇怪的API,所有事情都考虑在内。 –