如何编辑iOS8中的图像元数据? [英] How to edit metadata of image in iOS8?

查看:148
本文介绍了如何编辑iOS8中的图像元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
    }];

我使用上面的代码来获取image的元数据。但是现在我要编辑这个元数据。我想想要在{GPS}词典中没有位置信息的情况下在图像中添加自定义位置。

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.

推荐答案

来自apple的文档:只允许应用程序编辑他们最初编写的资产。因此,如果您的应用程序在照片库中编写图像,那么只有您能够编辑其元数据。

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.

您可以使用 ALAsset 可编辑来检查元数据是否可编辑 property。

You can check whether metadata is editable or not using ALAsset’s editable property.

我能够使用 setImageData:metadata:completionBlock:方法更新元数据。

I was able to update metadata using setImageData:metadata:completionBlock: method.

请参考以下代码:
我正在传递具有更新元数据的相同图像数据。此外,我测试了更改方向而不是GPS数据,但此代码可帮助您开始:

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");
    }

这篇关于如何编辑iOS8中的图像元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆