UIImagePickerController委托不称为Swift 3 [英] UIImagePickerController delegate not called Swift 3

查看:115
本文介绍了UIImagePickerController委托不称为Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从表面上看,我认为这必须是一个委托问题,但是在要求委托之后,就返回了正确的委托人.

On the surface I thought that this had to be a delegate issue, but after asking for the delegate the right one was returned.

我创建了一个ImagePicker类来处理所有UIImagePickerController的东西.每件事都起作用,直到需要调用委托方法为止.选完照片后,imagePicker消失了,但是didFinishPickingMediaWithInfo方法再也不会被调用.请帮忙!谢谢:)

I created an ImagePicker class to handle all the UIImagePickerController stuff. Every thing works until the delegate methods need to be called. After I pick a photo, the imagePicker dismisses, but the didFinishPickingMediaWithInfo method never gets called. Please help! Thanks :)

func selectPhoto() {
    imagePicker.delegate = self //Delegate gets set here

    let photoAsk = UIAlertController.init( //Ask user if they want to take picture or choose one
        title: "Edit Profile Picture",
        message: nil,
        preferredStyle: .alert)
    let cameraAction = UIAlertAction.init(
        title: "Take Photo",
        style: .default) { (UIAlertAction) in
            if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
                self.imagePicker.sourceType = .camera
                UIApplication.topViewController()!.present(self.imagePicker, animated: true, completion:nil)
            } else {
                print("Cannot access camera in simulator.")
                return
            }
    }
    let photoLibraryAction = UIAlertAction.init(
        title: "Photo Library",
        style: .default) { (UIAlertAction) in
            self.imagePicker.sourceType = .photoLibrary
            UIApplication.topViewController()!.present(self.imagePicker, animated: true, completion:nil)
            print("UIImagePickerDelegate: \(self.imagePicker.delegate.debugDescription)") // <--THIS PRINTS OUT "AppName.ImagePicker: 0x145d7bdf0>", and the class name is ImagePicker
    }
    let cancelAction = UIAlertAction.init(
        title: "Cancel",
        style: .cancel) { (UIAlertAction) in return }

    photoAsk.addAction(cameraAction)
    photoAsk.addAction(photoLibraryAction)
    photoAsk.addAction(cancelAction)

    imagePicker.mediaTypes = [kUTTypeImage as String]

    UIApplication.topViewController()?.present(photoAsk, animated: true, completion: nil)
    }
}

这永远不会被称为:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("Image picked.") //NEVER PRINTS
}

推荐答案

详细信息

  • Xcode 9.2,Swift 4
  • Xcode 10.2.1(10E1001),Swift 5
  • extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true, completion: nil)
        }
    
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            print("\(info)")
            if let image = info[.originalImage] as? UIImage {
                imageView?.image = image
                dismiss(animated: true, completion: nil)
            }
        }
    }
    

    用法

    let imagePickerController = UIImagePickerController()
    imagePickerController.allowsEditing = false
    imagePickerController.sourceType = .photoLibrary
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)
    

    完整样本

    不要忘记在此处添加解决方案代码(如上所示)

    import UIKit
    
    class ViewController: UIViewController {
    
        private weak var imageView: UIImageView?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let stackView = UIStackView(frame: .zero)
            stackView.axis = .vertical
            stackView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(stackView)
            stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
            stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    
            let imageView = UIImageView()
            imageView.contentMode = .scaleAspectFit
            imageView.translatesAutoresizingMaskIntoConstraints = false
            stackView.addArrangedSubview(imageView)
            imageView.widthAnchor.constraint(equalToConstant: 200).isActive = true
            imageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
            self.imageView = imageView
    
            let button = UIButton(frame: .zero)
            button.setTitle("Button", for: .normal)
            button.setTitleColor(.blue, for: .normal)
            button.addTarget(self, action: #selector(showImages), for: .touchUpInside)
            stackView.addArrangedSubview(button)
        }
    
        @IBAction func showImages(_ sender: AnyObject) {
            let imagePickerController = UIImagePickerController()
            imagePickerController.allowsEditing = false
            imagePickerController.sourceType = .photoLibrary
            imagePickerController.delegate = self
            present(imagePickerController, animated: true, completion: nil)
        }
    }
    

    这篇关于UIImagePickerController委托不称为Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆