Swift 4.2 imagePickerController issue

后端 未结 6 1710
清歌不尽
清歌不尽 2020-12-09 19:46

Trying to pass chat client from swift 4 to swift 4.2 and with picker i found trouble.

UIImagePickerControllerEditedImage Cannot subscript a value of

相关标签:
6条回答
  • 2020-12-09 19:50

    like this

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        picker.dismiss(animated: true, completion: nil)
        let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
        ...
    }
    
    0 讨论(0)
  • 2020-12-09 19:50
    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)
        }
    }
    

    Above code solved the warning and image successfully uploaded

    0 讨论(0)
  • 2020-12-09 19:53

    The method signature has changed to

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

    You should see a warning message for the func name

    Instance method 'imagePickerController(:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

    Candidate has non-matching type '(UIImagePickerController, [String : Any]) -> ()'

    Move 'imagePickerController(_:didFinishPickingMediaWithInfo:)' to another extension to silence this warning

    Make 'imagePickerController(_:didFinishPickingMediaWithInfo:)' private to silence this warning

    Requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' declared here (UIKit.UIImagePickerControllerDelegate)

    0 讨论(0)
  • 2020-12-09 19:57

    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")
        }
    }
    
    0 讨论(0)
  • 2020-12-09 20:00

    In Swift 4.2 imagePickerController delegate methods has changed a bit. Kindly check out the below code.

    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)
        }
    
    }
    

    This will work for sure.

    0 讨论(0)
  • 2020-12-09 20:09
    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!!")
            }
        }
    
    0 讨论(0)
提交回复
热议问题