LoginSignup
6
5

More than 5 years have passed since last update.

PHAssetから、UIImageを生成する

Last updated at Posted at 2019-01-18

背景

弊社製アプリケーションに、ライブラリからの写真選択機能を搭載しようとしたら、四苦八苦したという・・・

TL;DR (ソースコード載せとくね)

PHAssetからUIImageを生成する方法をまとめておきます。

let requestOption = PHContentEditingInputRequestOptions()
requestOption.isNetworkAccessAllowed = true

asset.requestContentEditingInput(with: requestOption) { (contentEditingInput: PHContentEditingInput?, _) -> Void in
    let ciImage = CIImage(contentsOf: contentEditingInput!.fullSizeImageURL!)!
    let exifDictionary = ciImage.properties
    let image = UIImage(ciImage: ciImage.oriented(forExifOrientation: contentEditingInput!.fullSizeImageOrientation))
    // anything you can what ever do you want.
}

注意点

クロージャ内部で受け取るcontentEditingInputは

open var fullSizeImageOrientation: Int32 { get } // EXIF value

を持ちます。

UIKitに慣れ親しんだ私はこれをUIImage.OrientationのValueだと勘違いして3日潰しましたが、この値は、

open func oriented(forExifOrientation orientation: Int32) -> CIImage

下記メソッドに投げるためのOrientationなので、めっっっっっっっっっっっっっっちゃ注意しようね!
(なんでコメントでEXIF Valueと書いてあるのか考えるべきだった)

6
5
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
6
5