【发布时间】:2018-01-17 18:38:55
【问题描述】:
我想要做的是显示一个警报,用户可以在其中按下从相机或照片库获取照片。但是,当我按下库时, UIImagePickerViewController 不存在。我正在向 ALert 添加 UIAlert 操作,并且我传入的处理程序是作为呈现 ImagePicker 控制器的函数。我究竟做错了什么?我怀疑这与我呈现 ImagePickerViewController 的方式有关,但我不确定。我阅读了苹果文档,他们说弹出框是唯一呈现它的样式。
import UIKit
import Firebase
class SignupViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var ProfilePic: UIButton!
//When button is pressed, alert will pop up
@IBAction func bringUpAlert(_ sender: Any) {
let alert = UIAlertController(title: "Profile Photo", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
func showCamera(){
let camera = UIImagePickerController()
UIImagePickerController.isSourceTypeAvailable(.camera)
camera.delegate = self
camera.sourceType = .camera
self.present(camera, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Library", style: .default, handler: {(action) -> Void in
func showLibrary(){
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let library = UIImagePickerController()
library.delegate = self
library.sourceType = .photoLibrary
library.modalPresentationStyle = UIModalPresentationStyle.popover
self.present(library, animated: true, completion: nil)
}
func imagePickerController(_picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String:Any]) {
let selectedImage = didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] as! UIImage
self.ProfilePic.setImage(selectedImage, for: .normal)
}
}
}))
self.present(alert, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
//Customizing Profile Pic View
ProfilePic.layer.cornerRadius = ProfilePic.frame.size.width / 2
ProfilePic.clipsToBounds = true
ProfilePic.layer.borderWidth = 3
ProfilePic.layer.borderColor = UIColor.gray.cgColor
}
}
【问题讨论】: