0

我的代码:

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
}

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

4

4 回答 4

11

您必须使用 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)
}
于 2016-07-17T23:34:41.510 回答
2

要在其中使用imagePickerController,您需要在Info.plistSwift3文件中添加带有 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)
    }

}

希望有帮助!

于 2017-02-03T03:36:30.700 回答
1

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

你可能想要

let image = info[UIImagePickerControllerOriginalImage] as? UIImage

或者

let image = info[UIImagePickerControllerEditedImage] as? UIImage
于 2016-06-18T09:02:52.033 回答
0
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

于 2017-10-29T00:10:23.913 回答