使用元数据将照片保存到具有特定文件名的相机胶卷中

时间:2017-04-20 08:45:38

标签: ios exif photos phasset phphotolibrary

我正在尝试使用以下代码使用exif元数据保存图像:

+(void)saveImageWithMetaToCameraRoll:(UIImage*)image location:(CLLocation*)location album:(PHAssetCollection *)album exif:(NSDictionary *)metadata isOriginal:(BOOL)isOriginal isFavorite:(BOOL)isFavorite completionBlock:(PHAssetBoolBlock)completionBlock {

NSMutableDictionary *meta = [metadata mutableCopy];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL);

if (!isOriginal) {
    [meta resetOrientation];
}

NSString *timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970] * 1000];
NSString *fileName = [NSString stringWithFormat:@"IMG_%@_%@.jpeg", timestamp, isOriginal ? @"orig" : @"d"];

[meta setTitle:fileName];

NSURL *tmpURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];

CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef) tmpURL, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef) meta);

CGImageDestinationFinalize(destination);
CFRelease(source);
CFRelease(destination);

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:tmpURL];

    PHObjectPlaceholder *placeholderAsset = newAssetRequest.placeholderForCreatedAsset;


    if (album) {
        PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];

        [changeRequest addAssets:@[placeholderAsset]];
    }

} completionHandler:^(BOOL success, NSError *error) {
    //Clean up the file:
    [[NSFileManager defaultManager] removeItemAtURL:tmpURL error:nil];

    if (error) {
        NSLog(@"%@", error);
    }

    completionBlock(success);
}];
}

注意到某些应用程序(Snapseed,Photoshop等)在将图像导出到相机胶卷时能够设置自定义文件名。我无法弄清楚他们究竟是如何做到这一点的。任何的想法?

PS:我已经尝试this way,但它在iOS 10.3.1

上无效

0 个答案:

没有答案