Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2017 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1e3075e8bc2b56118a1d9f348f14c2dc to your computer and use it in GitHub Desktop.
Save anonymous/1e3075e8bc2b56118a1d9f348f14c2dc to your computer and use it in GitHub Desktop.
-(void)saveImage:(UIImage * _Nonnull)image metadata:(NSDictionary * _Nullable)metadata
{
#if 0 // attach only Exif
NSDictionary *exif = metadata[@"{Exif}"];
metadata = (exif == nil)? nil : @{ @"{Exif}" : exif };
#endif
NSMutableData *jpegData = [[NSMutableData alloc] init];
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)jpegData, kUTTypeJPEG, 0, NULL);
CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)metadata);
BOOL writeSuccess = CGImageDestinationFinalize(imageDestination);
CFRelease(imageDestination);
if(!writeSuccess)
return;
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetCreationRequest *assetCreation = [PHAssetCreationRequest creationRequestForAsset];
[assetCreation addResourceWithType:PHAssetResourceTypePhoto data:jpegData options:nil];
}
completionHandler:^(BOOL success, NSError * _Nullable error) {
NSLog(@"%d, %@", success, error);
}
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment