关于 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/