将缩略图添加/传递(exif)到CGImageDestinationRef

ale*_*x-i 6 cocoa exif objective-c cgimage

CGImageDestinationRef Apple的参考文献中,它提到It can contain thumbnail images as well as properties for each image.具有属性的部分很好,通过properties在添加图像时设置参数CGImageDestinationRef,但我无法弄清楚如何添加缩略图.

我想添加一个缩略图(或直接从a传递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);
Run Code Online (Sandbox Code Playgroud)

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

有没有办法保留原始缩略图(或创建一个新缩略图)?CGImageProperties Reference似乎没有任何键来存储缩略图.

Nic*_*ald 1

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

指定字典如下

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

更新:为了将辅助(缩略图)图像添加到原始 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);
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)