Write UIImage along with metadata (EXIF, GPS, TIFF) in iPhone's Photo library

后端 未结 8 1793
情歌与酒
情歌与酒 2020-11-29 05:12

I am developing a project, where the requirements are: - User will open the camera through the application - Upon capturing an Image, some data will be appended to the captu

8条回答
  •  感情败类
    2020-11-29 05:29

    Here is a slight variation of @matt answer.

    The following code use only one CGImageDestination and more interesting allow to save in HEIC format on iOS11+.

    Notice that the compression quality is added to the metadata before adding the image. 0.8 is roughly the compression quality of native camera save.

    //img is the UIImage and metadata the metadata received from the picker
    NSMutableDictionary *meta_plus = metadata.mutableCopy;
    //with CGimage, one can set compression quality in metadata
    meta_plus[(NSString *)kCGImageDestinationLossyCompressionQuality] = @(0.8);
    NSMutableData *img_data = [NSMutableData new];
    NSString *type;
    if (@available(iOS 11.0, *)) type = AVFileTypeHEIC;
    else type = @"public.jpeg";
    CGImageDestinationRef dest = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)img_data, (__bridge CFStringRef)type, 1, nil);
    CGImageDestinationAddImage(dest, img.CGImage, (__bridge CFDictionaryRef)meta_plus);
    CGImageDestinationFinalize(dest);
    CFRelease(dest); //image is in img_data
    //go for the PHLibrary change request
    

提交回复
热议问题