Swift 3 - 无法覆盖'imagePickerController'

Posted

技术标签:

【中文标题】Swift 3 - 无法覆盖\'imagePickerController\'【英文标题】:Swift 3 - Cannot override 'imagePickerController'Swift 3 - 无法覆盖'imagePickerController' 【发布时间】:2016-06-18 08:46:49 【问题描述】:

我的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) 
    self.image = image
    self.imageStatusLabel.text = "There is a picture"
    self.imageStatusLabel.textColor = UIColor.blue()

    picker.dismiss(animated: true, completion: nil)

这是错误:

无法覆盖已标记为不可用的“imagePickerController”:在 ios 7 及更早版本中弃用的 API 在 Swift 中不可用

好吧,我已经尝试使用更新的功能:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    // But no image in the parameters

但是,参数中没有图像,我可以获取图像。 我该怎么办?

【问题讨论】:

【参考方案1】:

您必须使用 info 参数并转换为 UIImage。不要忘记您必须在info.plist 文件中输入“Privacy - Photo Library Usage Description”和“Privacy - Camera Usage Description”,并用字符串填充值,说明请求访问照片库和相机的原因。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage 
        pictureImageView.contentMode = .scaleToFill
        pictureImageView.image = pickedImage
    

    picker.dismiss(animated: true, completion: nil)

【讨论】:

【参考方案2】:

要在Swift3 中使用imagePickerController,您需要添加一行键:Privacy - Photo Library Usage Description,值为string(例如:“访问您的照片”)到Info.plist 文件,让我们在下面尝试:

class YourController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

    let imagePickerButton: UIButton = 
        let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
        btn.setTitle("Choose a Photo", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .blue
        btn.addTarget(self, action: #selector(handlePickerDidTap), for: .touchUpInside)

        return btn
    ()

    override func viewDidLoad() 
        super.viewDidLoad()
        view.addSubview(imagePickerButton)
    

    func handlePickerDidTap() 
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = true
        present(imagePickerController, animated: true, completion: nil)
    

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
        print("photo is selected")
        // do some thing here
    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
        dismiss(animated: true, completion: nil)
    


希望对您有所帮助!

【讨论】:

【参考方案3】:

您从传递的info 字典中获取图像,查看documentation 中的键

你可能想要一个

let image = info[UIImagePickerControllerOriginalImage] as? UIImage

let image = info[UIImagePickerControllerEditedImage] as? UIImage

【讨论】:

【参考方案4】:
import UIKit
import Photos

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

@IBOutlet weak var imageView: UIImageView!

@IBAction func selectPhotoLibrary(_ sender: Any) 
    let picker = UIImagePickerController()
    picker.delegate = self
    if UIImagePickerController.isSourceTypeAvailable(.camera) 
        picker.sourceType = .camera
     else
        print("Kamera desteklenmiyor.")
    
    present(picker, animated: true, completion:nil)


@IBAction func selectPhotoCamera(_ sender: Any) 
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion:nil)


override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    picker.dismiss(animated: true, completion:nil)
    guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else  return 
    guard let imageData = UIImageJPEGRepresentation(image, 0.8) else  return 
    self.imageView.image = image
    print(imageData)


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
    picker.dismiss(animated: true, completion:nil)


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  

阅读更多使用细节 --> https://medium.com/@yakupad/swift-uiimagepickercontroller-kullanımı-91771ed4c1d6

【讨论】:

以上是关于Swift 3 - 无法覆盖'imagePickerController'的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 - 无法覆盖'imagePickerController'

swift ImagePicker,第一次迭代

Swift ImagePicker StartVideoCapture

如何使用 Swift/SwiftUI 从 ImagePicker 获取选定的图像文件名

UIImagePickerController 委托未调用 Swift 3

UIImagePickerController委托不叫Swift 3