Ios 当我展示我的UIImagePickerController时,什么都没有出现
我试图做的是显示一个警报,其中用户将从相机或照片库中获取照片。但是,当我按library时,UIImagePickerViewController不存在。我正在将UIAlert操作添加到警报中,我要传递的处理程序是一个函数,它显示ImagePicker控制器。我做错了什么?我怀疑这与我如何展示ImagePickerViewController有关,但我不确定。我读了苹果公司的文档,他们说只有popover才能展示它Ios 当我展示我的UIImagePickerController时,什么都没有出现,ios,swift,Ios,Swift,我试图做的是显示一个警报,其中用户将从相机或照片库中获取照片。但是,当我按library时,UIImagePickerViewController不存在。我正在将UIAlert操作添加到警报中,我要传递的处理程序是一个函数,它显示ImagePicker控制器。我做错了什么?我怀疑这与我如何展示ImagePickerViewController有关,但我不确定。我读了苹果公司的文档,他们说只有popover才能展示它 import UIKit import Firebase class Sign
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
}
}
图像选择器从未显示。在动作警报中,从不调用函数
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:
func doStuff(x: Int, handler: (Int) -> Void) {
handler(x)
}
doStuff(x: 3) {val in
print(val)
}
--output:--
3
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>
示例2:
func doStuff(x: Int, handler: (Int) -> Void) {
handler(x)
}
doStuff(x: 3) {val in
print(val)
}
--output:--
3
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>
func-doStuff(x:Int,handler:(Int)->Void){
处理器(x)
}
doStuff(x:3){val-in
函数计算(a:Int,b:Int){
打印(a+b)
}
}
--输出:--
iOS编程不适合新手程序员,对有经验的程序员来说很难。
UIImagePickerController.isSourceTypeAvailable(.camera)
——尽管这完全符合文档中的说明,但它什么都不做。@7stud谢谢。更新。这是对原始代码的盲目复制和粘贴。@maddy,是的,我知道。很高兴能帮上忙。作为一个新用户,我想提醒您,您可以通过单击最能回答您问题的答案左侧的复选标记来表示您的问题已经解决。享受