swift 4 中未弹出选择相机或画廊的警报视图

alert view to pick camera or gallery doesn't pop up in swift 4

我有一个带导航控制器的 viewcontroller。我有上传按钮 images.But 按下按钮后它不会弹出相机或照片库选项的警报,尽管我已经编写了警报代码。为什么不出现警报弹出窗口?我还在 info.plist 中给出了隐私 - 照片库使用说明。给出了我的代码:

import UIKit
import  Alamofire

class ProfilePicController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


var imagePicker = UIImagePickerController()


@IBOutlet weak var uploadPic: UIButton!
@IBOutlet weak var profilePic: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()

  imagePicker.delegate = self
  profilePic.layer.cornerRadius = profilePic.frame.size.width / 2
  profilePic.clipsToBounds = true


    // Do any additional setup after loading the view.
}


@IBAction func uploadPic(_ sender: Any) {

 print("image add")

let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
  print("Camera selected")
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera

})
alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
  print("Photo selected")
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

})

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

}
 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
 let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

 profilePic.image = selectedImage
 // self.savePic()
 dismiss(animated: true, completion: nil)
 }

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

//Alert.swift 文件

import UIKit

class Alert {

class func showBasic(title: String ,message: String , vc: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(defaultAction)
vc.present(alert, animated: true, completion: nil)
}
}

实际上你没有显示警报

@IBAction func uploadPic(_ sender: Any) {

  print("image add")

  let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

  alert.addAction(UIAlertAction(title: "Camera", style: .default) { (result : UIAlertAction) -> Void in
    print("Camera selected")
    self.imagePicker.sourceType = .camera
    self.present(self.imagePicker, animated: true, completion: nil)
  })
  alert.addAction(UIAlertAction(title: "Photo library", style: .default) { (result : UIAlertAction) -> Void in
    print("Photo selected")
    self.imagePicker.sourceType = .photoLibrary
    self.present(self.imagePicker, animated: true, completion: nil)
  })

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

}