在CGImageDestinationRef苹果的参考它提到,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);
好吧,既然没有人回应(即使在赏钱期间),再加上我花了几个小时试图传递缩略图,我猜CGImageDestination的文档是误导性的。似乎没有任何(有记录的)方法将图像缩略图传递给CGImageDestinationRef
(至少不包括OSX 10.7.0和iOS 5.0)。
如果有人不这么认为,请贴出答案,我将接受它(如果它是正确的)。
在ios8.0 +中有一个半文档化的属性用于嵌入EXIF缩略图:kCGImageDestinationEmbedThumbnail.
苹果的开发者文档是空的,但是头文件(CGImageDestination.h
)包含以下注释:
/* Enable or disable thumbnail embedding for JPEG and HEIF.
* The value should be kCFBooleanTrue or kCFBooleanFalse. Defaults to kCFBooleanFalse */
IMAGEIO_EXTERN const CFStringRef kCGImageDestinationEmbedThumbnail IMAGEIO_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
但是你不能指定任意的缩略图数据,它会自动为你创建。