swift
下記のコードでカメラロールから写真を取得しようとしているのですが、画像が表示されません。カメラロール画面が開き、画像を選択することまでは出来るのですが、その画像がviewcontroller?storyboard?に表示されません。
その解決策と原因を教えてくださると嬉しいです。おねがいします!
コード
@IBAction func accessCamerarole(_ sender: Any) {
pickImageFromLibrary()
}
func pickImageFromLibrary() { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { //追記 //写真ライブラリ(カメラロール)表示用のViewControllerを宣言しているという理解 let controller = UIImagePickerController() //おまじない引用テキスト controller.delegate = (self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate) //新しく宣言したViewControllerでカメラとカメラロールのどちらを表示するかを指定 //以下はカメラロールの例 //.Cameraを指定した場合はカメラを呼び出し(シミュレーター不可) controller.sourceType = UIImagePickerControllerSourceType.photoLibrary //新たに追加したカメラロール表示ViewControllerをpresentViewControllerにする self.present(controller, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String: Any]) { //おまじない if didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] != nil { //didFinishPickingMediaWithInfo通して渡された情報(選択された画像情報が入っている?)をUIImageにCastする //そしてそれを宣言済みのimageViewへ放り込む imageFromCameraRoll.image = didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] as? UIImage } //写真選択後にカメラロール表示ViewControllerを引っ込める動作 picker.dismiss(animated: true, completion: nil) func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return .landscape } }-
回答1件
あなたの回答
tips
プレビュー