Nic*_*son 2 core-foundation ios
我正在尝试复制 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;
}
Run Code Online (Sandbox Code Playgroud)
根据 Apple 的 HeaderDoc 文件中的CGImageDestinationCopyImageSource()函数文档<ImageIO/CGImageDestination.h>:
之后不应调用 CGImageDestinationFinalize() - 当此函数返回时,结果将保存到目的地。
| 归档时间: |
|
| 查看次数: |
795 次 |
| 最近记录: |