【问题标题】:Print UIImage metadata to label打印 UIImage 元数据到标签
【发布时间】:2015-06-11 14:47:37
【问题描述】:

我正在尝试读取从 UIImagePicker 拍摄时由 UIImage 生成的元数据,但遇到了一些问题。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// get the metadata
NSDictionary *imageMetadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
NSLog (@"imageMetaData %@",imageMetadata);

这成功地将元数据字典打印到 NSLog。

我遇到的问题是我想访问字典的特定索引(例如 FNumber、ISO 等)并将它们打印到特定标签,但我不知道如何访问个人数据。

这是我迄今为止尝试提取数据的方法,但它似乎没有找到密钥(它返回为 NULL):

NSLog(@"ISO: %@", imageMetadata[@"ISOSpeedRatings"]);

根据 NSLog 为字典打印的内容,似乎字典中可能存在字典,这就是让我失望的原因。

以下是为元数据打印的内容:

imageMetaData {
DPIHeight = 72;
DPIWidth = 72;
Orientation = 6;
"{Exif}" =     {
    ApertureValue = "2.27500704749987";
    BrightnessValue = "-0.6309286396300304";
    ColorSpace = 1;
    DateTimeDigitized = "2015:04:01 10:33:37";
    DateTimeOriginal = "2015:04:01 10:33:37";
    ExposureBiasValue = 0;
    ExposureMode = 0;
    ExposureProgram = 2;
    ExposureTime = "0.06666666666666667";
    FNumber = "2.2";
    Flash = 24;
    FocalLenIn35mmFilm = 29;
    FocalLength = "4.15";
    ISOSpeedRatings =         (
        320
    );
    LensMake = Apple;
    LensModel = "iPhone 6 back camera 4.15mm f/2.2";
    LensSpecification =         (
        "4.15",
        "4.15",
        "2.2",
        "2.2"
    );
    MeteringMode = 5;
    PixelXDimension = 3264;
    PixelYDimension = 2448;
    SceneType = 1;
    SensingMethod = 2;
    ShutterSpeedValue = "3.907056515078773";
    SubjectArea =         (
        1631,
        1223,
        1795,
        1077
    );
    SubsecTimeDigitized = 705;
    SubsecTimeOriginal = 705;
    WhiteBalance = 0;
};
"{MakerApple}" =     {
    1 = 2;
    14 = 0;
    2 = <0f000b00 06000900 04005000 a900b100 b700bb00 c400cd00 cd00a400 b100c700 14000b00 05000900 06000a00 8a00a800 b000b800 c300cb00 c900cd00 b300a600 2f000700 06000700 0a000400 3500a400 ab00b300 bc00c300 cf00d300 b4007f00 3f000700 09000700 0a000700 05007100 a100af00 b500c200 ce00cd00 a9006b00 1f000a00 0b000900 0a000c00 05001e00 9c00aa00 b400c200 cc00d000 d4005700 2b001900 0d001000 10000d00 08000600 5b00a700 b300bf00 cb00d500 e3008600 eb002800 1a001700 14000c00 0b000700 10009400 b100c000 ce00e000 f400bd00 cf013e00 2a001200 17000f00 0d000800 07004200 b100c000 d300e900 fd000401 ff011101 1d000700 16001400 09000700 07000900 8900bf00 d800ec00 07011f01 10021102 39000b00 10001900 0e000800 0a000700 2c00bf00 dd00f400 0b012401 1e023802 1f010d00 07001900 16000c00 0c000800 21007000 c500f400 0a012e01 10022202 01022500 08001000 18001100 0d001800 1601cc00 d100eb00 09012201 fb011002 26020401 0f000700 16001400 3200e801 6001b000 ce00f400 08011601 e1010602 1a020302 23001700 21002300 84009300 9f00ad00 bf00e800 02011401 ca01fc01 19024002 08013d00 3500ca00 7c009200 9e00ab00 c200d700 f8000a01 b401f101 1b024802 28023000 4a007f00 7f008e00 a000b100 bd00d000 ec00fe00>;
    3 =         {
        epoch = 0;
        flags = 1;
        timescale = 1000000000;
        value = 777291330499583;
    };
    4 = 1;
    5 = 128;
    6 = 123;
    7 = 1;
    8 =         (
        "0.2226092",
        "-0.5721548",
        "-0.7796207"
    );
    9 = 275;
};
"{TIFF}" =     {
    DateTime = "2015:04:01 10:33:37";
    Make = Apple;
    Model = "iPhone 6";
    Software = "8.1.2";
    XResolution = 72;
    YResolution = 72;
};

}

我要在另一个名为 Exif 的 NSDictionary 中查找的数据吗?如果是这样,我该如何访问它?

【问题讨论】:

标签: ios objective-c uiimage nsdictionary uiimagepickercontroller


【解决方案1】:

@david strauss 你能试试这个吗

- (void) saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info{
// Get the assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Get the image metadata (EXIF & TIFF)
NSMutableDictionary * imageMetadata = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy];

// add GPS data
CLLocation * loc = <•••>; // need a location here
if ( loc ) {
    [imageMetadata setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary];
}

ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
    if (error) {
        NSLog( @"Error writing image with metadata to Photo Library: %@", error );
    } else {
        NSLog( @"Wrote image %@ with metadata %@ to Photo Library",newURL,imageMetadata);
    }
};

// Save the new image to the Camera Roll
[library writeImageToSavedPhotosAlbum:[imageToSave CGImage] 
                             metadata:imageMetadata 
                      completionBlock:imageWriteCompletionBlock];
[imageMetadata release];
[library release];

}

【讨论】:

  • 这使我可以将图像及其元数据写入照片库,但现在我如何访问该数据以进行打印?
【解决方案2】:

您已经注意到元数据字典由几个字典组成。 所以回答你的问题 - 是的,如果你正在寻找特定的值,你可以访问内部字典。此外,您最好使用 ImageIO 常量中的正确键;例如:

NSLog(@"%@", imageMetadata[(NSString*)kCGImagePropertyExifDictionary][(NSString*)kCGImagePropertyExifISOSpeedRatings]);

或者,您可以使用键路径:

NSString *keyPath = [NSString stringWithFormat:@"%@.%@",
                         (NSString*)kCGImagePropertyExifDictionary, (NSString*)kCGImagePropertyExifISOSpeedRatings];
NSLog(@"%@", [imageMetadata valueForKeyPath:keyPath]);

【讨论】:

    【解决方案3】:

    您所有需要的数据都只在这本字典里。

    您可以使用 json 格式化程序查看数据的确切位置。

    NSError *error; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject: imageMetadata 
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"jsonString: %@", jsonString);
    }
    

    在此之后从 json 中获取数据的确切位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多