将UIImage元数据打印到标签

时间:2015-04-07 05:15:44

标签: ios objective-c uiimage nsdictionary uiimagepickercontroller

我试图在从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中寻找的数据是什么?如果是这样,我该如何访问它?

3 个答案:

答案 0 :(得分:0)

@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];

}

答案 1 :(得分:0)

您注意到的元数据字典包含多个字典。 所以回答你的问题 - 是的,如果你正在寻找具体的价值,你可以访问内部词典。此外,您最好使用ImageIO常量中的正确键;例如:

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

或者,您可以使用密钥路径:

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

答案 2 :(得分:0)

您所需的所有数据仅在此词典中。

您可以使用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获取数据的确切位置