[iOS] UIImagePickerControllerで選択した画像のメタデータを取得してみた

2016.12.16

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんぬづは、最近の毎週の楽しみがドラマ「逃げるは恥だが役に立つ」になっている田中です。
応用情報と基本情報とデータベーススペシャリストを取ればガッキーを雇えるということですね、わかりました。(なにもわかっていない

今回はUIImagePickerControllerを使って画像のメタデータを取得する方法の紹介です。

動機

以下の記事でExifについて調べてみて、「iOSでこういった情報を取るにはどうするんだろう」と思ったことがきっかけです。

取得してみた

プロジェクト全体のサンプルをGitHubに公開しました。記事内で要所の解説は行いますが、より詳しく見たい方はこちらのリポジトリをご参照ください。

info.plistにPhoto Libraryへのアクセス目的を明記する

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>画像を使いますヨ!</string>
    ......
    ...
</dict>
</plist>

iOS 10からユーザーデータへアクセスする際にはその用途を明記することが必須になりました。詳しくはこちらの記事を参照ください。

画像を読み込む箇所でPhotosをimportする

今回はPhotosフレームワークを利用するので、使用する箇所でPhotosをimportします。

import Photos

UIImagePickerControllerで選択した画像からメタデータを取得する

画像のメタデータはCIImageのインスタンスのpropertiesプロパティから取得することができます。サンプルのソースコードを実行すると中身をprintすることができるので、よりイメージがつきやすいかもしれません。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
    // 選択したアイテムの元のバージョンのAssets Library URL
    let assetUrl = info[UIImagePickerControllerReferenceURL] as! URL
    
    // PHAsset = Photo Library上の画像、ビデオ、ライブフォト用の型
    let result = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil)
    
    let asset = result.firstObject
    
    // コンテンツ編集セッションを開始するためのアセットの要求
    asset?.requestContentEditingInput(with: nil, completionHandler: { contentEditingInput, info in
        // contentEditingInput = 編集用のアセットに関する情報を提供するコンテナ
        let url = contentEditingInput?.fullSizeImageURL
        // 対象アセットのURLからCIImageを生成
        let inputImage = CIImage(contentsOf: url!)!
        
        // CIImageのpropertiesから画像のメタデータを取得する
        // 横幅 - 4032
        let pixelWidth  = inputImage.properties["PixelWidth"]
        // 縦幅 - 3024
        let pixelHeight = inputImage.properties["PixelHeight"]
        // 横幅dpi - 72
        let dpiWidth    = inputImage.properties["DPIWidth"]
        // 縦幅dpi - 72
        let dpiHeight   = inputImage.properties["DPIHeight"]
        // ??? - 8
        let depth       = inputImage.properties["Depth"]
        
        // プロファイル名 #とは - sRGB IEC61966-2.1
        let profileName = inputImage.properties["ProfileName"]
        // カラーモデル - RGB
        let colorModel  = inputImage.properties["ColorModel"]
        // 画像の向き(角度) - 1
        let orientation = inputImage.properties["Orientation"]
        
        // 各種規格
        // Exif
        let exif    = inputImage.properties["{Exif}"]
        // ExifAux
        let exifAux = inputImage.properties["{ExifAux}"]
        // GPS
        let gps     = inputImage.properties["{GPS}"]
        // TIFF
        let tiff    = inputImage.properties["{TIFF}"]
        // IPTC
        let iptc    = inputImage.properties["{IPTC}"]
        // JFIF
        let jfif    = inputImage.properties["{JFIF}"]
    })
}

まとめ

ざっくりではありますが、以下の手順をおさえておけばあとはUIImagePickerControllerの基本的な使い方の部分になります。

  • Photo Libraryへアクセスする場合はinfo.plistにその用途を明記する
  • Photosをimportする
  • UIImagePickerControllerで選択した画像からメタデータを取得する

今回はUIImagePickerControllerとPhotosフレームワークを用いて試しましたが、単純な画像のメタデータ取得であればCIImageから取れるようなのでもっとシンプルなものになりそうですね。そちらも今後まとめてみようと思います。

参考・関連