代码之家  ›  专栏  ›  技术社区  ›  Adam

IOS Swift ImagePickerControl未调用

  •  0
  • Adam  · 技术社区  · 6 年前
    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并弹出照片库

    但选择照片不打电话 imagePickerController

    选择照片后不执行任何操作

    如何在swift 4中使用imagepicker?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Sharad Chauhan Muhammad Bilal Hussain    6 年前

    删除 @objc Delegate 方法和 delegate 方法是这样的,请用以下内容替换函数:

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

    并通过以下方式获取图像:

    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {return}
    
        2
  •  -1
  •   Rahul Patil    6 年前
    import UIKit
    

    类ViewController:UIViewController、UIImagePickerControllerDelegate、UINavigationControllerDelegate{

    let imageView = UIImageView()
    let imagepicker = UIImagePickerController()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.imagepicker.delegate = self
        imageView.frame = CGRect(x: 40, y: 150, width: 100, height: 100)
        self.view.addSubview(imageView)
    }
    
    @IBAction func uploadImage(_ sender : UIButton) {
        imagepicker.cameraDevice = .rear
        imagepicker.cameraCaptureMode = .photo
        imagepicker.sourceType = .photoLibrary
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            return
        }
        imageView.image = image
        print(image)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }
    

    }