UIImagePickerController: didFinishPickingMediaWith

2019-06-22 00:06发布

Hope you're all doing well. Wondered if anyone could help me understand why Xcode fails to play nice with me....

I have broken the code into sections below in order to allow me to select a profile photo from UIImagePicker:

  1. WORKS: Assigning the delegate class for both ImagePicker & NavigationController
  2. WORKS: Instantiating the UIImagePicker variable
  3. WORKS: Load ImagePickerViewController and enable to select an image
  4. FAILS: Added additional functions for closing and finishing picking with a UIImagePicker but neither ever get called? What have I done wrong?

Thanks in advance!


class ProfileVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker: UIImagePickerController!

override func viewDidLoad() {
    super.viewDidLoad()

    imagePicker = UIImagePickerController()
    imagePicker.delegate = self
}

@IBAction func selectPhotoPressed(sender: UITapGestureRecognizer) {
    if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
        self.imagePicker.sourceType = .PhotoLibrary
    } else {
        self.imagePicker.sourceType = .Camera
    }
    imagePicker.allowsEditing = true
    imagePicker.mediaTypes = [kUTTypeImage as String]
    self.presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo image: UIImage, editingInfo: [String : AnyObject]?) {
    headerView.profilePhoto.contentMode = .ScaleAspectFill
    headerView.profilePhoto.image = image
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    uploadImage(image)
}

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-22 00:16

OK I seem to have it working now, thanks for your help in identifying that the code was ancient ;) Here is what now runs:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    headerView.profilePhoto.contentMode = .ScaleAspectFit
    headerView.profilePhoto.image = image
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    uploadImage(image!)
}
查看更多
爷、活的狠高调
3楼-- · 2019-06-22 00:25

You have the signature wrong, but since it's an optional method, you are not getting warned. It should be:

func imagePickerController(picker: UIImagePickerController,
          didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    // Your code
}
查看更多
冷血范
4楼-- · 2019-06-22 00:32

As everyone suggested, use the didfinishpickingmediawithinfo method and to extract the image in swift 3, ios 10, use the info dictionary

func imagePickerController(picker: UIImagePickerController,
          didFinishPickingMediaWithInfo info: [String : AnyObject]) 
{
    imageView.image =  info[UIImagePickerControllerOriginalImage] as? UIImage
}
查看更多
登录 后发表回答