从表面上看,我认为这必须是一个代表问题,但在要求代表后,正确的一个被退回.
我创建了一个ImagePicker类来处理所有UIImagePickerController的东西.在需要调用委托方法之前,每件事都有效.在我选择照片后,imagePicker会解散,但是didFinishPickingMediaWithInfo方法永远不会被调用.请帮忙!谢谢 :)
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,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,completion: nil) } }
永远不会被称为:
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : Any]) { print("Image picked.") //NEVER PRINTS }
解决方法
细节
Xcode 9.2,Swift 4
解
extension BaseViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate { func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { dismiss(animated: true,completion: nil) } func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : Any]) { NSLog("\(info)") let image = info[UIImagePickerControllerOriginalImage] as? UIImage imagePickerController(picker,pickedImage: image) } @objc func imagePickerController(_ picker: UIImagePickerController,pickedImage: UIImage?) { } }
完整样本
BaseViewController.swift
import UIKit class BaseViewController: UIViewController { let imagePicker = UIImagePickerController() override func viewDidLoad() { super.viewDidLoad() imagePicker.delegate = self } } extension BaseViewController: UIImagePickerControllerDelegate,pickedImage: UIImage?) { } }
ViewController.swift
import UIKit class ViewController: BaseViewController { 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) { imagePicker.allowsEditing = false imagePicker.sourceType = .photoLibrary present(imagePicker,completion: nil) } override func imagePickerController(_ picker: UIImagePickerController,pickedImage: UIImage?) { if let image = pickedImage { imageView?.image = image dismiss(animated: true,completion: nil) } } }