Swift UIImagePickerController didFinishPickingMediaWithInfo 未触发

Posted

技术标签:

【中文标题】Swift UIImagePickerController didFinishPickingMediaWithInfo 未触发【英文标题】:Swift ImagePickerController didFinishPickingMediaWithInfo not fired 【发布时间】:2016-11-08 06:46:52 【问题描述】:

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

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)



为什么不调用代码?

【问题讨论】:

【参考方案1】:

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

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


您可以在UIImagePickerControllerDelegate 上查看Apple Documentations 了解更多详情。

【讨论】:

【参考方案2】:

在 Swift 4 中,该方法再次具有不同的签名,将 String 替换为 UIImagePickerController.InfoKey:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])


【讨论】:

以上是关于Swift UIImagePickerController didFinishPickingMediaWithInfo 未触发的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式访问 ipad“视频”文件夹

在 iPhone 上捕获的视频,试图将其移动到另一个目录

swift [Swift Notes]在学习Swift #Swift的同时收集笔记

Swift 反射

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift