【问题标题】:alert view to pick camera or gallery doesn't pop up in swift 4swift 4中不会弹出选择相机或画廊的警报视图
【发布时间】:2018-08-26 19:42:59
【问题描述】:

我有一个带有导航控制器的视图控制器。我有上传图像的按钮。但按下按钮后,它不会弹出相机或照片库选项的警报,尽管我已经编写了警报代码。为什么不出现警报弹出窗口?我还在 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)
}
}

【问题讨论】:

    标签: ios swift4 uiimagepickercontroller alert


    【解决方案1】:

    其实你并没有显示警报

    @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)
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2013-04-29
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多