Add / Transfer (exif) Thumbnail CGImageDestinationRef

In CGImageDestinationRef, Apple Link mentions that It can contain thumbnail images as well as properties for each image. The properties part works well by setting the properties parameter when adding an image to CGImageDestinationRef , but I cannot figure out how to add a thumbnail.

I want to add a thumbnail (or pass it directly from CGImageSourceRef ) to CGImageDestinationRef , so that when the resulting image is opened with CGImageSourceRef , I can use CGImageSourceCreateThumbnailAtIndex to retrieve the original sketch.

 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); 

The above code returns a thumbnail of the images that it saved, but when I pass the image through CGImageDestinationRef , the thumbnail will be lost.

Is there a way to keep the original sketch (or create a new one)? The CGImageProperties link does not seem to have keys for storing thumbnails.

+6
source share
2 answers

One of the ways I found the sketch was to specify kCGImageSourceCreateThumbnailFromImageIfAbsent in the settings dictionary.

Specify the dictionary as follows

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

Update. To add a secondary (thumbnail) image to the original CGImageDestinationRef, do the following after adding the main image

 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); 

Then, to return the sketch, follow these steps

 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); 
+1
source

Well, since no one answered (even during generosity), plus after I spent time sketching, I assume that the documentation for CGImageDestination is misleading. It seems that some (documented) way to transfer the thumbnail of the image to CGImageDestinationRef (at least not up to OSX 10.7.0 and iOS 5.0, including those included in it).

If someone thinks differently, send an answer, and I will accept it (if he is right).

+1
source

Source: https://habr.com/ru/post/904977/


All Articles