imagePickerControllerで選択した動画/写真のOptionalをBinding

 

環境

  • swift
  • swift3
  • xcode
  • xcode9
  • imagePickerController

実装

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //アルバムから選択した動画を変数に代入
        let documentsPath = info[UIImagePickerControllerMediaURL] as! URL!
        //urlをstringに変換
        var urlString: String = documentsPath!.absoluteString
        //オプショナルを取り外す
        let optionalString: String? = urlString
        if let unwrapString = optionalString {
        //stringをurlに変換して代入
        videoURL = URL(string: unwrapString)
    }