从swift库中选择照片不起作用

问题描述 投票:0回答:1

在我的应用程序中,我有一个“addImgBtn”,应该让用户从他的照片中选择图像。我也有“profileImg”存储一些临时图像。

当我点击“addIng”按钮时,它会显示所有照片,但是当我点击“选择”时,它不会用所选的一个替换临时图像,这里是我的代码:

class myClass: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var addImgBtn: UIButton!

@IBAction func addImgBtn(_ sender: Any) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
    self.dismiss(animated: true, completion: { () -> Void in

    })

    profileImg.image = image
 }
}
swift uiimage uiimagepickercontroller
1个回答
1
投票

您使用的是错误的imagePickerController方法,请更改为以下内容:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    self.dismiss(animated: true, completion: { () -> Void in
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            self.profileImg.image = image
        }
    })
}

这应该工作!作为一般提示,命令 - 单击进入UIImagePickerControllerDelegate或您在xcode中实现的任何委托,它将告诉您所有可用的方法等。

© www.soinside.com 2019 - 2024. All rights reserved.