I have created a custom camera using AVFoundation
, now after capturing images, I need to save them in the iPhone's gallery.
I tried saving images with UIImageWriteToSavedPhotosAlbum
but found that this does not save EXIF information.
I have created a custom camera using AVFoundation
, now after capturing images, I need to save them in the iPhone's gallery.
I tried saving images with UIImageWriteToSavedPhotosAlbum
but found that this does not save EXIF information.
To Save EXIF information with an image refer below code.
- (void)saveImageDataToPhotoAlbum:(NSData *)originalData
{
NSDictionary *dataDic = [self getDataAndMetadata:originalData];
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib writeImageDataToSavedPhotosAlbum:dataDic[@"data"]
metadata:dataDic[@"metadata"]
completionBlock:^(NSURL *url, NSError *e) {
[self addToMyAlbum:url];
}];
}
- (NSDictionary *)getDataAndMetadata:(NSData *)originalData
{
CGImageSourceRef cimage = CGImageSourceCreateWithData((CFDataRef)originalData, nil);
NSDictionary *metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cimage, 0, nil);
NSMutableDictionary *metadataAsMutable = [NSMutableDictionary dictionaryWithDictionary:metadata];
metadataAsMutable[(NSString *)kCGImagePropertyGPSDictionary] = self.myGpsDic;
NSMutableData *dataForMetadataRemoval = [NSMutableData data];
CGImageDestinationRef dest =
CGImageDestinationCreateWithData((CFMutableDataRef)dataForMetadataRemoval, CGImageSourceGetType(cimage), 1, nil);
CGImageDestinationAddImageFromSource(dest, cimage, 0, (CFDictionaryRef)metadataAsMutable);
CGImageDestinationFinalize(dest);
CFRelease(dest);
[metadata release];
CFRelease(cimage);
return @{ @"data" : (dataForMetadataRemoval), @"metadata" : metadataAsMutable};
}
return metadataAsMutable;
}