objective-c - 添加/传递(exif)缩略图到 CGImageDestinationRef

标签 objective-c cocoa exif cgimage

关于 CGImageDestinationRef Apple 的引用资料中提到 It can contain thumbnail images as well as properties for each image.通过设置 properties,具有属性的部分效果很好将图像添加到 CGImageDestinationRef 时的参数, 但我不知道如何添加缩略图。

我想添加缩略图(或直接从 CGImageSourceRef 传递)到 CGImageDestinationRef , 这样当用 CGImageSourceRef 打开生成的图像时我可以使用 CGImageSourceCreateThumbnailAtIndex检索原始缩略图。

NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
                                       (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                                       (id)kCFBooleanFalse, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                                       [NSNumber numberWithInt:128], (id)kCGImageSourceThumbnailMaxPixelSize, 
                                       nil];
CGImageRef thumb = CGImageSourceCreateThumbnailAtIndex(iSource, 0, (CFDictionaryRef)thumbOpts);

上面的代码返回存储它的图像的缩略图,但是当我通过 CGImageDestinationRef 传递图像时, 缩略图丢失了。

有什么方法可以保留原始缩略图(或创建新缩略图)? CGImageProperties Reference似乎没有任何键可以存储缩略图。

最佳答案

我发现获取缩略图的一种方法是在设置字典中指定 kCGImageSourceCreateThumbnailFromImageIfAbsent。

指定字典如下

NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
                               (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                               (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                               [NSNumber numberWithInt:128], (id)kCGImageSourceThumbnailMaxPixelSize,
                               nil];

更新:为了向原始 CGImageDestinationRef 添加辅助(缩略图)图像,请在添加主图像后执行以下操作

size_t thumbWidth = 640;
size_t thumbHeight = imageHeight / (imageWidth / thumbWidth);
size_t thumbBytesPerRow = thumbWidth * 4;
size_t thumbTotalBytes = thumbBytesPerRow * thumbHeight;
void *thumbData = malloc(thumbTotalBytes);
CGContextRef thumbContext = CGBitmapContextCreate(thumbData,
                                                  thumbWidth,
                                                  thumbHeight,
                                                  8,
                                                  thumbBytesPerRow,
                                                  CGColorSpaceCreateDeviceRGB(),
                                                  kCGImageAlphaNoneSkipFirst);

CGRect thumbRect = CGRectMake(0.f, 0.f, thumbWidth, thumbHeight);
CGContextDrawImage(thumbContext, thumbRect, fullImage);
CGImageRef thumbImage = CGBitmapContextCreateImage(thumbContext);
CGContextRelease(thumbContext);
CGImageDestinationAddImage(destination, thumbImage, nil);
CGImageRelease(thumbImage);

然后为了恢复缩略图,请执行以下操作

CFURLRef imageFileURLRef = (__bridge CFURLRef)url;
NSDictionary* sourceOptions = @{(id)kCGImageSourceShouldCache: (id)kCFBooleanFalse,
                                    (id)kCGImageSourceTypeIdentifierHint: (id)kUTTypeTIFF};
CFDictionaryRef sourceOptionsRef = (__bridge CFDictionaryRef)sourceOptions;
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imageFileURLRef, sourceOptionsRef);
CGImageRef thumb = CGImageSourceCreateImageAtIndex(imageSource, 1, sourceOptionsRef);
UIImage *thumbImage = [[UIImage alloc] initWithCGImage:thumb];
CFRelease(imageSource);
CGImageRelease(thumb);

关于objective-c - 添加/传递(exif)缩略图到 CGImageDestinationRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8711366/

相关文章:

iphone - 为什么以及何时应该在 iOS 应用程序中使用 C/C++ 代码?

multithreading - 枚举时集合发生突变,导致 NSTableView、NSArrayController 和 NSThread 崩溃

cocoa - 允许应用程序在工作表可见时终止

ios - 哪个函数用于获取 DJI Drone for iOS 的 GPS 坐标的确切位置

objective-c - 为什么我不应该释放这个字符串?

尝试读取图像元数据时,Python XMP Toolkit 过于严格 ("Unrecognized TIFF prefix")

Java从ICC_Profile中提取colorSpace和PCS来验证图像的完整性

python - 从 Tiff 图像中获取描述/关键字?

objective-c - 无法理解 Swift 中的特定运算符

cocoa - OS X 中的辅助单击切换