Writing a Photo with Metadata using Photokit

耗尽温柔 提交于 2019-12-01 01:47:06

I figured it out after some research. Here is how you save image to Document Directory with Exif.

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObjects:myAssetURL, nil] options:nil];
        [savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
          PHImageRequestOptions *cropToSquare = [[PHImageRequestOptions alloc] init];
          cropToSquare.synchronous = YES;
          [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                            options:cropToSquare
                                                      resultHandler:
           ^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
             CIImage* ciImage = [CIImage imageWithData:imageData];
             NSMutableDictionary *metadataAsMutable = [ciImage.properties mutableCopy];
             CGImageSourceRef source = Nil;
             NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:dataUTI ,kCGImageSourceTypeIdentifierHint,nil];
             source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData,  (__bridge CFDictionaryRef) sourceOptionsDict);
             CFStringRef UTI = CGImageSourceGetType(source);
             NSMutableData *dest_data = [NSMutableData data];
             CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);
             CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable);
             BOOL success = NO;
             success = CGImageDestinationFinalize(destination);

             if(!success) {
             }
             [dest_data writeToFile:myTrumbImageFileName atomically:YES];
             CFRelease(destination);
             CFRelease(source);
           }];
        }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!