web-dev-qa-db-ja.com

Swift 4.2 imagePickerControllerの問題

チャットクライアントをSwift 4からSwift 4.2に切り替え、ピッカーで問題が見つかりました。

UIImagePickerControllerEditedImage型の値に添字を付けることはできません '[String:Any]'型のインデックス 'UIImagePickerController.InfoKey'

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        self.userProfileImage.contentMode = .scaleAspectFit
        self.userProfileImage.image = pickedImage
    }

    picker.dismiss(animated: true, completion: nil)
}
10
George Heints

メソッドのシグネチャが次のように変更されました

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

Func名に関する警告メッセージが表示されるはずです

インスタンスメソッド「imagePickerController(:didFinishPickingMediaWithInfo :)」は、プロトコル「UIImagePickerControllerDelegate」のオプション要件「imagePickerController(:didFinishPickingMediaWithInfo :)」にほぼ一致します。

候補に一致しないタイプ '(UIImagePickerController、[String:Any])->()'

この警告を消すには、「imagePickerController(_:didFinishPickingMediaWithInfo :)」を別の拡張機能に移動します

'imagePickerController(_:didFinishPickingMediaWithInfo :)'をプライベートにして、この警告を消します。

ここで宣言されている要件 'imagePickerController(_:didFinishPickingMediaWithInfo :)'(UIKit.UIImagePickerControllerDelegate)

18
Ashley Mills

In Swift 4.2 imagePickerControllerデリゲートメソッドは少し変更されました。以下のコードをご覧ください。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    var selectedImage: UIImage?
    if let editedImage = info[.editedImage] as? UIImage {
        selectedImage = editedImage
        self.profileImage.image = selectedImage!
        picker.dismiss(animated: true, completion: nil)
    } else if let originalImage = info[.originalImage] as? UIImage {
        selectedImage = originalImage
        self.profileImage.image = selectedImage!
        picker.dismiss(animated: true, completion: nil)
    }

}

これは確実に機能します。

9
Purnendu roy

このような

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    picker.dismiss(animated: true, completion: nil)
    let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
    ...
}
4
yaoning

Swift4.2

 //MARK:- ImagePicker Controller Delegate
 //MARK:-

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let chosenImage = info[.originalImage] as? UIImage {
        yourimage.contentMode = .scaleAspectFill
        yourimage.image = chosenImage

    } else{
        print("Something went wrong")
    }
}
2
Maulik Patel
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            yourImage.image = pickedImage
        }else{
            print("Something went wrong!!")
        }
    }
1
SCS
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let chosenImage = info[.originalImage] as? UIImage {
            selectedImage = chosenImage
            photo.image = chosenImage
        }
        dismiss(animated: true, completion: nil)
    }
}

上記のコードは警告を解決し、画像は正常にアップロードされました

0
sara