xcode Swift 3 - 无法覆盖“imagePickerController”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37895049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:56:02  来源:igfitidea点击:

Swift 3 - Cannot override 'imagePickerController'

xcodeswiftuiimagepickercontrollerswift3xcode8

提问by Etgar

My code:

我的代码:

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)
}

And this is the error:

这是错误:

Cannot override 'imagePickerController' which has been marked unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift

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

Well, I've tried to use the newer function:

好吧,我尝试使用较新的功能:

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

But then, there is no image in the parameters and I can get the image.
What should I do?

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

回答by Kaique Pantosi D'amato

You have to use the info parameter and make a cast to UIImage. Don't forget that you have to put a "Privacy - Photo Library Usage Description" and a "Privacy - Camera Usage Description" in the info.plistfile and fill the values with a string explaining the reason for request the access to photo library and camera.

您必须使用 info 参数并强制转换为UIImage. 不要忘记,您必须在文件中放置一个“ Privacy - Photo Library Usage Description”和“ Privacy - Camera Usage Description”,info.plist并用一个字符串填充这些值,解释请求访问照片库和相机的原因。

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)
}

回答by javimuu

To use imagePickerControllerin Swift3you need add a row with key: Privacy - Photo Library Usage Descriptionand value is a string(e.g: "Access to your photos") to Info.plistfile and let's try below:

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

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)
    }

}

Hope it help!

希望有帮助!

回答by Sulthan

You get the image from the passed infodictionary, see the keys in the documentation

您从传递的info字典中获取图像,请参阅文档中的键

You probably want either

你可能想要

let image = info[UIImagePickerControllerOriginalImage] as? UIImage

or

或者

let image = info[UIImagePickerControllerEditedImage] as? UIImage

回答by Yakup Ad

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.
  }
}

Read more for usage detail --> https://medium.com/@yakupad/swift-uiimagepickercontroller-kullan?m?-91771ed4c1d6

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