本文介绍了Swift ImagePickerController didFinishPickingMediaWithInfo未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ImagePickerController来获取个人资料图片.用户有一个按钮.该按钮触发CamRoll功能.单击该按钮时,将显示普通的ImagePickerViewController,但不会调用didFinishPickingMediaWithInfo.在早期版本的Swift/Xcode中,调用了相同的代码.这是我的代码.

I use the ImagePickerController, to grab a profile image. The user has a button. The button triggers the CamRoll function. When the button is clicked, the normal ImagePickerViewController appears, but the didFinishPickingMediaWithInfo isn't called. In an earlier version of Swift/Xcode, the same code was called. This is my code.

import UIKit

class AnnonseViewController: UIViewController, UIImagePickerControllerDelegate {

@IBOutlet var Image: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

}

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

}

@IBAction func CamRoll(_ sender: Any) {

    let imagePicker = UIImagePickerController()

    imagePicker.delegate = self

    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

    self.present(imagePicker, animated: true, completion: nil)

}

private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [AnyHashable: Any]) {
    var image = info[UIImagePickerControllerOriginalImage] as! UIImage

    let imageSize = image.size
    let width = imageSize.width
    let height = imageSize.height

    if width != height {
        let newDimensions = min(width,height)
        let widthOffset = (width - newDimensions) / 2
        let heightOffset = (height - newDimensions) / 2

        UIGraphicsBeginImageContextWithOptions(CGSize(width: newDimensions, height: newDimensions), false, 0.0)

        image.draw(at: CGPoint(x: -widthOffset, y: -heightOffset), blendMode: .copy, alpha: 1.0)

        image = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
        UIGraphicsEndImageContext()

    }

    UIGraphicsBeginImageContext(CGSize(width: 150, height: 150))

    _ = UIGraphicsGetCurrentContext()

    image.draw(in: CGRect(x: 0, y: 0, width: 150, height: 150))

    let smallImage = UIGraphicsGetImageFromCurrentImageContext()

    Image.image = smallImage

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


}

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

}

为什么不调用代码?

推荐答案

在Swift 3中,对于imagePickerController(_:didFinishPickingMediaWithInfo:)UIImagePickerControllerDelegate正确的委托方法如下.

The correct delegate method of UIImagePickerControllerDelegate in Swift 3 for imagePickerController(_:didFinishPickingMediaWithInfo:) is as follow.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

}

您可以查看UIImagePickerControllerDelegate上的 Apple文档.

这篇关于Swift ImagePickerController didFinishPickingMediaWithInfo未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:43