Saving photo with metadata into Camera Roll with specific file name

怎甘沉沦 提交于 2019-12-22 06:41:12

问题


I'm trying to save an image with exif metadata using following code:

+(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);
}];
}

Noticed that some apps (Snapseed, Photoshop and others) are able to set custom file name when they export an image into Camera Roll. And I can't figure out how exactly they do this. Any idea?

PS: I've already tried this way and it didn't work on iOS 10.3.1

来源:https://stackoverflow.com/questions/43514516/saving-photo-with-metadata-into-camera-roll-with-specific-file-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!