Currently I'm changing over from ALAssetsLibrary to PHPhotoLibrary.
I want to add or overwrite GPS metadata on JPEG file without losing any other data.
With ALAssetsLibrary, we could do it like below
- (void)saveImageDataToPhotoAlbum:(NSData *)originalData
{
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
NSDictionary *dataDic = [self getMetaDataWithGps:originalData];
[assetsLib writeImageDataToSavedPhotosAlbum:originalData
metadata:[self getMetadataWithGpsInfo:originalData]
completionBlock:^(NSURL *url, NSError *e) {
[self addToMyAlbum:url];
}];
}
- (NSDictionary *)getMetadataWithGpsInfo:(NSData *)originalData
{
CGImageSourceRef cimage = CGImageSourceCreateWithData((CFDataRef)data, nil);
NSDictionary *metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cimage, 0, nil);
NSMutableDictionary *metadataAsMutable = [NSMutableDictionary dictionaryWithDictionary:metadata];
metadataAsMutable[(NSString *)kCGImagePropertyGPSDictionary] = self.myGpsDic;
[metadata release];
CFRelease(cimage);
return metadataAsMutable;
}
But with PHPhotoLibrary I cannot do the same thing.
I tried to create NSData(made from JPEG file) with adding or overwriting GPS metadata. I could create and save image data with GPS metadata. But it lost thumbnail data and other data(such as maker note). Moreover the file size([data length]) reduced from about 5MB to about 3MB.
My code to create image data with GPS metadata is like below,
- (NSData *)getDataWithGpsInfo:(NSData *)originalData
{
CGImageSourceRef cimage = CGImageSourceCreateWithData((CFDataRef)data, nil);
NSDictionary *metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cimage, 0, nil);
NSMutableDictionary *metadataAsMutable = [NSMutableDictionary dictionaryWithDictionary:metadata];
metadataAsMutable[(NSString *)kCGImagePropertyGPSDictionary] = self.myGpsDic;
NSMutableData *dataWithLocationInfo = [NSMutableData data];
CGImageDestinationRef dest =
CGImageDestinationCreateWithData((CFMutableDataRef)dataWithLocationInfo, CGImageSourceGetType(cimage), 1, nil);
CGImageDestinationAddImageFromSource(dest, cimage, 0, (CFDictionaryRef)metadataAsMutable);
CGImageDestinationFinalize(dest);
CFRelease(dest);
[metadata release];
CFRelease(cimage);
return dataWithLocationInfo;
}
In case of ALAssetsLibraray, the file size before/after adding GPS metadata is almost same (5MB).
I looked for and tried several ways like the following questions but they cannot resolve this issue.
If you have any good idea, please let me know. I'd appreciate any information about this issue.