How to edit metadata of image in iOS8?

匿名 (未验证) 提交于 2019-12-03 09:10:12

问题:

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.

回答1:

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’s editable 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 :

ALAsset *asset = // your asset     if(asset.editable) {         NSDictionary *metadata = asset.defaultRepresentation.metadata;          NSDictionary *gpsInfo=metadata[@"{GPS}"];          if (gpsInfo) {             NSMutableDictionary *mutableGPSInfo = [gpsInfo mutableCopy];             [mutableGPSInfo setObject:@"yourNewLatitude" forKey:@"Latitude"];             [mutableGPSInfo setObject:@"yourNewLongitude" forKey:@"Longitude"];              NSMutableDictionary *mutableMetadata = [metadata mutableCopy];             [mutableMetadata setObject:[mutableGPSInfo copy] forKey:@"{GPS}"];              ALAssetRepresentation *rep = [asset defaultRepresentation];             Byte *buffer = (Byte*)malloc(rep.size);             NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];             NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];              [asset setImageData:data metadata:[mutableMetadata copy] completionBlock:^(NSURL *assetURL, NSError *error) {                 if (error) {                     NSLog(@"Error : %@",error);                 } else {                     NSLog(@"Asset metadata is successfully edited");                 }             }];         }     } else {         NSLog(@"oops..! Asset can not be edited");     } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!