ios - UIImagePickerController导入图像

标签 ios swift uikit

我想从库中导入 ios 中的图像,但出现错误

value of type 'viewcontroller' has no member 'present'

class ViewController: UIViewController {

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


    @IBAction func Alert(sender: AnyObject) {

        let nextController = UIImagePickerController()
        self.present(nextController, animated: true, completion: nil)


    }}

最佳答案

这是我的剥离工作代码。我使用两个选择器 - 一个用于相机,一个用于库 - 因为一些奇怪的 iOS 色调错误。为了避免“大量 View Controller ”问题,我倾向于将与 UIImagePickerController 相关的所有可能内容移动到单独的文件和扩展名中。

class OpenViewController: UIViewController {

    let pickerLibrary = UIImagePickerController()
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerLibrary.delegate = self

    }
}

extension OpenViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

   @objc func showImagePicker() {
        pickerLibrary.allowsEditing = false
        pickerLibrary.sourceType = .photoLibrary
        present(pickerLibrary,
                animated: true,
                completion: nil)
        pickerLibrary.popoverPresentationController?.sourceView = self.view
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        dismiss(animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: false, completion: nil)
    }
}

虽然present代码看起来正确,但我注意到您没有在代码中发布一些内容。也许是因为你没有发布它,但我肯定会检查这些内容:

  • 您是否将 ViewController 设置为委托(delegate)?
  • 您是否将 ViewController.view 设置为源 View ? iPhone 可能不需要,但 iPad 肯定有帮助。
  • 您是否包含了 UIImagePickerControllerDelegate、UINavigationControllerDelegate 所需的所有内容?

现在,正如您的错误所暗示的那样,可能是其他问题 - UIViewController 可以 呈现另一个 View Controller 。在这种情况下,您没有给我们提供重复该错误的方法。换句话说,我假设 (1) 您看到一个附加到 Alert()UIButton - 顺便说一句,Swift 约定使用小写字母 - 并且 (2 )您已经证明,当点击该按钮时,实际上会执行“Alert()”。

关于ios - UIImagePickerController导入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575827/

相关文章:

ios - 使用动画调整 UITableView header 大小的问题

ios - 从 Swift 中的 Objective C 集合调用 block

ios - 视觉限制未被识别

iphone - 为什么 iPhone 上没有 NSAttributedString?

swift - 如何在 Swift 中将 UIColor 用作枚举类型的 RawValue

ios - 如何在 iOS 中找到当前 View Controller 之前的所有 View Controller ?

ios - 将代码更新到 xcode 9 和 swift 4 后应用程序崩溃

ios - 整体加入了UIBezierPath笔画

ios - 单击 UIActionSheet 按钮后应用程序挂起

ios - 更改 View 的父级