my segue is not working in imagepicker - swift

I have an UIImagePickerController so when user choose his/her photo the segue is performed but segue is not working ( actually is working but the vc is not loaded ) this is my code:
let imagepicker = UIImagePickerController()
imagepicker.delegate = self
imagepicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagepicker.allowsEditing = true
self.present(imagepicker, animated: true)
//...
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
takenimage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.dismiss(animated: true, completion: nil)
print("image loaded")
performSegue(withIdentifier: "ToPrescriptionView", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ToPrescriptionView" {
let PrescriptionView = segue.destination as! Prescription
PrescriptionView.PrescriptionImage = takenimage
print("segue loaded")
}
I'm getting "image loaded" and "segue loaded" but my viewcontroller remains previous one

Try to performSegue inside completion block of dismiss when you dismissing UIImagePickerController.
self.dismiss(animated: true) {
performSegue(withIdentifier: "ToPrescriptionView", sender: nil)
}

Related

UIImagePickerController Function Not Working Correctly for Camera, Works for Photo Album

I'm writing a function to add an image to a UIImageView with UIImagePickerController. Once the image is added, a Share button is then enabled - both the camera and the photo-album let me pick an image and add it to the UIImageView, but the share button is only enabled when I pick an image from the Photo Album. When I pick an image from the Camera, the button doesn't work. Any idea why? Here is the relevant code:
//Pick Image from Album
#IBAction func pickAnImageFromAlbum(_ sender: Any) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = .photoLibrary
pickerController.allowsEditing = true
present(pickerController, animated: true, completion: nil)
}
//Pick Image from Camera
#IBAction func pickAnImageFromCamera(_ sender: Any) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = .camera
pickerController.allowsEditing = true
present(pickerController, animated: true, completion: nil)
}
//Send image to UIImageViewer, enable Share Button
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage {
shareButton.isEnabled = true
imageViewer.image = image
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}

Pass Captured/Gallery Image from one ViewController to another

I want to capture a photo or pick it from the gallery and then pass it inside an imageView to another ViewController. I make the sequel successfully but I don't know how to pass the image from the imagePickerController.
First ViewController
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[.originalImage] as? UIImage{
imagePicker.dismiss(animated: true){
self.performSegue(withIdentifier: "goToCropScreen", sender: self)
}
}
}
//Navigation to other screens
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToCropScreen"{
let destinationVC = segue.destination as! CropViewController
destinationVC.imageToCrop = //Here is the problem. Idont know what image to use.
}
}
Second ViewController
class CropViewController: UIViewController {
#IBOutlet weak var cropImageView: UIImageView!
var imageToCrop : UIImage?
override func viewDidLoad() {
super.viewDidLoad()
cropImageView.image = imageToCrop
}
}
I know that I can't use selectedImage because its inside the imagePickerController. Is there a way to use it like globally.
Create a variable in FirstViewController. And store the selected image in this variable and pass the value in prepare for segue method
class FirstViewController: UIViewController {
var selectedImage: UIImage?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[.originalImage] as? UIImage{
imagePicker.dismiss(animated: true){
self.selectedImage = selectedImage
self.performSegue(withIdentifier: "goToCropScreen", sender: self)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToCropScreen"{
let destinationVC = segue.destination as! CropViewController
destinationVC.imageToCrop = self.selectedImage
}
}
}

UIImagePickerController crashes with LongPressGesture

Today I realized that my UIImagePickerController is working fine when I use it with a button to present it. However, when I want to present it via LongPressGesture, the app crashes on runtime. Why so?
Action:
#IBAction func ppLongPressed(_ sender: UILongPressGestureRecognizer) {
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
Delegate:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
The long gesture has states
#IBAction func ppLongPressed(_ sender: UILongPressGestureRecognizer) {
if sender.state != .began { return }
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}

Why doesn't my View disappear after removing from parent ViewController in Swift?

I am making an app using sprite kit, so my scenes/views are all SKView optionals. However I am trying to allow the user to upload/take photos, and everything works well on that end. However, after the user selects a photo, my image view controller somehow gets animated again, and the scene that I was in before no longer enables interaction, so it is basically frozen. I am confused why this happens... this is what I have tried so far:
Scene that I am in when giving option for selecting a photo
func photoToController(sender: UIButton){
var button = sender
let controller = imageViewController()
sender.superview?.removeFromSuperview()
view?.addSubview(controller.view)
controller.pickPhoto(sender: &image, button: &imageUploader, buttonsender: &button)
}
ViewController that allows user to deal with photo library/camera
class imageViewController : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var image = UIImageView()
var Abutton = UIButton()
var takingPhoto = false
override func viewDidLoad() {
print("imgviewloaded")
}
override func viewDidAppear(_ animated: Bool) {
print("image view is animated")
}
func pickPhoto(sender: inout UIImageView, button: inout UIButton, buttonsender: inout UIButton) {
let types = [UIImagePickerControllerSourceType.photoLibrary,UIImagePickerControllerSourceType.camera]
if UIImagePickerController.isSourceTypeAvailable(types[buttonsender.tag]) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = types[buttonsender.tag]
if buttonsender.tag == 1 {takingPhoto = true
imagePicker.allowsEditing = false
} else {
imagePicker.allowsEditing = true
}
image = sender
Abutton = button
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
self.removeFromParentViewController()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
if takingPhoto{image.contentMode = .scaleToFill}
if image.image == info[UIImagePickerControllerOriginalImage] as? UIImage{
savedPhotos.saveNewPhoto(newPhoto : image.image!)
}
image.backgroundColor = UIColor.white
Abutton.setTitle("", for: .normal)
picker.dismiss(animated: true, completion: nil)
self.removeFromParentViewController()
}
}

Memory leaks when using UIImagePickerController in camera mode

This is a simple single view, single ViewController test app. This app has one button in the ViewController. Accessing the PhotoLibrary does not show any leaks, but the camera shows some leaks when presented and more when dismissed.
Here is code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let pickerCntrl = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
pickerCntrl.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func takePic(_ sender: Any) {
pickerCntrl.sourceType = UIImagePickerControllerSourceType.camera
pickerCntrl.cameraCaptureMode = .photo
pickerCntrl.allowsEditing = true
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
self.present(pickerCntrl, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// let editedCapture = info[UIImagePickerControllerEditedImage] as! UIImage?
// let origCapture = info[UIImagePickerControllerOriginalImage] as! UIImage?
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
ScreenShot :