【问题标题】:How to edit metadata of image in iOS8?如何在 iOS8 中编辑图像的元数据?
【发布时间】:2015-09-07 11:10:14
【问题描述】:
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);
    }];

我正在使用上面的代码来获取图像的元数据。但是现在我想编辑这个元数据。如果位置信息不存在于 {GPS} 字典中,我想在图像中添加自定义位置。

【问题讨论】:

    标签: objective-c iphone ios8 uiimagepickercontroller alassetslibrary


    【解决方案1】:

    来自苹果的文档:Applications are only allowed to edit assets that they originally wrote. 因此,如果您的应用程序正在照片库中写入图像,那么只有您能够编辑其元数据。

    您可以使用ALAsseteditable 属性检查元数据是否可编辑。

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2020-05-05
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      相关资源
      最近更新 更多