ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib assetForURL:nil resultBlock:^(ALAsset *asset) {
NSDictionary *metadata = rep.metadata;
if (metadata) {
NSDictionary *GPSDict=metadata[@"{GPS}"];
NSDictionary *TIFFDict=metadata[@"{TIFF}"];
if (GPSDict){
double longitude = [[GPSDict objectForKey:@"Longitude"] doubleValue];
double latitude = [[GPSDict objectForKey:@"Latitude"] doubleValue];
if ([[GPSDict objectForKey:@"LatitudeRef"] isEqualToString:@"S"]) {
latitude = -latitude;
}
if ([[GPSDict objectForKey:@"LongitudeRef"] isEqualToString:@"W"]) {
longitude = -longitude;
}
if (TIFFDict){
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
[pref setObject:[TIFFDict objectForKey:@"DateTime"] forKey:@"PHOTODATE"];
[pref synchronize];
}
coordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
}else {
latitude = locationManager.location.coordinate.latitude;
longitude = locationManager.location.coordinate.longitude;
[GPSDictionary setObject:[NSNumber numberWithFloat:fabs(latitude)]
forKey:(NSString*)kCGImagePropertyGPSLatitude];
[GPSDictionary setObject:(latitude > 0 ? @"N": @"S") forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
[GPSDictionary setObject:[NSNumber numberWithFloat:fabs(longitude)]
forKey:(NSString*)kCGImagePropertyGPSLongitude];
[GPSDictionary setObject:(longitude > 0 ? @"E": @"W") forKey:(NSString*)kCGImagePropertyGPSLongitudeRef]; //
if (metadata&& GPSDictionary) {
[metadata setValue:GPSDictionary forKey:(NSString*)kCGImagePropertyGPSDictionary];
}
coordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
}
}
else
{
}
} failureBlock:^(NSError *error) {
//User denied access
NSLog(@"Unable to access image: %@", error);
}];
I am using above code to get metadata of image.But now i want to edit this metadata.I want to add custom location in image if the location information is not present in the {GPS} dictionary.
From the apple’s documentation :
Applications are only allowed to edit assets that they originally wrote.
So if your application is writing image in Photos Library then only you will be able to edit its metadata.You can check whether metadata is editable or not using
ALAsset
’seditable
property.I was able to update metadata using
setImageData:metadata:completionBlock:
method.Please refer following code : I am passing same image data with updated metadata. Also I have tested changing orientation not the GPS data but this code helps you to start :