12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像ファイルからexifメタデータを抜き出す

Posted at

なんかだか面倒くさいけど、これライブラリないんだろうか。

static NSDictionary *getExifFromData(NSData * const data)
{
    CFStringRef imageSourceOptionKeys[2];
    CFTypeRef imageSourceOptionValues[2];
    imageSourceOptionKeys[0] = kCGImageSourceShouldCache;
    imageSourceOptionValues[0] = kCFBooleanTrue;
    imageSourceOptionKeys[1] = kCGImageSourceShouldAllowFloat;
    imageSourceOptionValues[1] = kCFBooleanTrue;

    CFDictionaryRef imageSourceOptions = CFDictionaryCreate(
            NULL,
            (const void **)imageSourceOptionKeys,
            (const void**)imageSourceOptionValues,
            sizeof(imageSourceOptionKeys) / sizeof(imageSourceOptionKeys[0]),
            &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks);


    CGImageSourceRef imageSourceRef = CGImageSourceCreateWithData((__bridge CFDataRef) data,  imageSourceOptions);
    CFRelease(imageSourceOptions);

    CFDictionaryRef imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);

    NSDictionary *exif = (__bridge NSDictionary *)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyExifDictionary);

    CFRelease(imageSourceRef);

    return exif;
}

うまくいくと以下の様なメタデータがとれる。

// NSLog("exif: %@, exif);
exif: {
    ColorSpace = 1;
    ComponentsConfiguration =     (
        1,
        2,
        3,
        0
    );
    DateTimeDigitized = "2012:10:15 20:57:48";
    DateTimeOriginal = "2012:10:15 20:57:48";
    ExifVersion =     (
        2,
        2,
        1
    );
    FlashPixVersion =     (
        1,
        0
    );
    PixelXDimension = 1536;
    PixelYDimension = 1536;
    SceneCaptureType = 0;
}

参考文献:

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?