Swift ImagePicker - 视图控制器
Swift ImagePicker - View controller
怎么了,
所以这是我从照片库中选择图像的代码,现在我想将用户发送到下一个视图控制器,但只有在用户选择图像之后。我的问题是:
如何让它转到下一个视图控制器,但只有在从照片库中选择了图像之后?
使完成按钮仅在用户选择图像后显示
或者,如果他们尝试单击完成,直到从库中选择图像,则只显示错误
谢谢!!!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let imageTap = UITapGestureRecognizer(target: self, action: #selector(openImagePicker))
ProfilePictureImageView.isUserInteractionEnabled = true
ProfilePictureImageView.addGestureRecognizer(imageTap)
ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.bounds.height / 2
ProfilePictureImageView.clipsToBounds = true
TapToChangeButton.addTarget(self, action: #selector(openImagePicker), for: .touchUpInside)
//when user clicks they can choose a photo from library
//instantiate image picker
ImagePicker = UIImagePickerController()
ImagePicker.allowsEditing = true
ImagePicker.sourceType = .photoLibrary
ImagePicker.delegate = self
setUpElements()
}
func setUpElements() {
ErrorLabel.alpha = 0
}
// taping to change and add a photo
@objc func openImagePicker(_ sender:Any) {
// Open Image Picker
self.present(ImagePicker, animated: true, completion: nil)
}
@IBAction func FinishTapped(_ sender: Any) {
}
}
//extend the proper delagate method
extension BackroundCheckViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//cancel
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//get the image the selected
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.ProfilePictureImageView.image = pickedImage
//upload to firbase
PhotoService.savePhoto(image: pickedImage)
}
picker.dismiss(animated: true, completion: nil)
}
}
在 UIImagePickerControllerDelegate
的 didFinishPickingMediaWithInfo
方法中,在选择器解除的完成块中添加导航到另一个视图控制器的代码。
extension BackroundCheckViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//...
picker.dismiss(animated: true) {
// add you code here to navigate to somewhere else.
let myViewController = MyViewController()
if let navigationController = self.navigationController {
navigationController.pushViewController(myViewController, animated: true)
} else {
self.present(myViewController, animated: true)
}
}
}
}
怎么了,
所以这是我从照片库中选择图像的代码,现在我想将用户发送到下一个视图控制器,但只有在用户选择图像之后。我的问题是:
如何让它转到下一个视图控制器,但只有在从照片库中选择了图像之后?
使完成按钮仅在用户选择图像后显示 或者,如果他们尝试单击完成,直到从库中选择图像,则只显示错误
谢谢!!!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let imageTap = UITapGestureRecognizer(target: self, action: #selector(openImagePicker))
ProfilePictureImageView.isUserInteractionEnabled = true
ProfilePictureImageView.addGestureRecognizer(imageTap)
ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.bounds.height / 2
ProfilePictureImageView.clipsToBounds = true
TapToChangeButton.addTarget(self, action: #selector(openImagePicker), for: .touchUpInside)
//when user clicks they can choose a photo from library
//instantiate image picker
ImagePicker = UIImagePickerController()
ImagePicker.allowsEditing = true
ImagePicker.sourceType = .photoLibrary
ImagePicker.delegate = self
setUpElements()
}
func setUpElements() {
ErrorLabel.alpha = 0
}
// taping to change and add a photo
@objc func openImagePicker(_ sender:Any) {
// Open Image Picker
self.present(ImagePicker, animated: true, completion: nil)
}
@IBAction func FinishTapped(_ sender: Any) {
}
}
//extend the proper delagate method
extension BackroundCheckViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//cancel
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//get the image the selected
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.ProfilePictureImageView.image = pickedImage
//upload to firbase
PhotoService.savePhoto(image: pickedImage)
}
picker.dismiss(animated: true, completion: nil)
}
}
在 UIImagePickerControllerDelegate
的 didFinishPickingMediaWithInfo
方法中,在选择器解除的完成块中添加导航到另一个视图控制器的代码。
extension BackroundCheckViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//...
picker.dismiss(animated: true) {
// add you code here to navigate to somewhere else.
let myViewController = MyViewController()
if let navigationController = self.navigationController {
navigationController.pushViewController(myViewController, animated: true)
} else {
self.present(myViewController, animated: true)
}
}
}
}