class MyPage: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
@IBOutlet weak var selectImage: UIButton!
override func viewDidLoad() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
}
@IBAction func selectImage(_ sender: Any) {
print("a")
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
print("b")
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true,completion: nil)
}
@objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
print("dismiss")
})
print(image)
selectImage.setImage(image, for: .normal)
}
}
如果触摸按钮,则打印a和b并弹出photoLibrary
但选择照片不致电
imagePickerController
选择照片后不执行任何操作
如何在Swift 4中使用ImagePicker?
最佳答案
从@objc
方法中删除Delegate
并且delegate
方法是这个,所以用以下代码替换函数:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
并使用此获取图像:
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {return}
关于ios - iOS Swift imagePickerController无法调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51471062/