我尝试做的是提供警报,用户可以预先从相机或照片库中获取照片。但是,当我按下库时,UIImagePickerViewController不会出现。我正在向ALert添加UIAlert动作,我传入的处理程序是一个呈现ImagePicker控制器的函数。我究竟做错了什么?我怀疑它与我如何呈现ImagePickerViewController有关,但我不确定。我读了苹果文档,他们说popover是展示它的唯一风格。
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
}
}
答案 0 :(得分:1)
您实际上从未尝试过显示图像选择器。您不必在警报操作中定义函数showCamera
,但您从不调用它。
正确的解决方案是删除该功能。
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
let camera = UIImagePickerController()
UIImagePickerController.isSourceTypeAvailable(.camera)
camera.delegate = self
camera.sourceType = .camera
self.present(camera, animated: true, completion: nil)
}))
或者,将showCamera
函数声明移到外面并从警报操作中调用它。
func showCamera(){
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let camera = UIImagePickerController()
camera.delegate = self
camera.sourceType = .camera
self.present(camera, animated: true, completion: nil)
}
}
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
showCamera()
}))
您需要对嵌入式showLibrary
函数执行相同操作。并且图像选择器委托方法也需要移动到类的顶层。
最后一堂课:
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
self.showCamera()
}))
alert.addAction(UIAlertAction(title: "Library", style: .default, handler: {(action) -> Void in
self.showLibrary()
}))
self.present(alert, animated: true, completion: nil)
}
func showCamera(){
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let camera = UIImagePickerController()
camera.delegate = self
camera.sourceType = .camera
self.present(camera, animated: true, completion: nil)
}
}
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)
}
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
}
}
答案 1 :(得分:0)
示例1:
func doStuff(x: Int, handler: (Int) -> Void) {
handler(x)
}
doStuff(x: 3) {val in
print(val)
}
--output:--
3
示例2:
func doStuff(x: Int, handler: (Int) -> Void) {
handler(x)
}
doStuff(x: 3) {val in
func calc(a: Int, b: Int) {
print(a + b)
}
}
--output:--
<Nothing>
iOS程序设计并不适合初学程序员 - 对于有经验的程序员来说很难。