how to show viewcontroller: UIImagePickController and dismiss it (Swift) - ios

i got this uiactionsheet which loads uipickerviewcontroller
its in a viewcontroller thats in a UISplitViewController
It's a UIsplitviewcontroller, this code is on a detailview, the detailview is called from masterview
how ever, when i try to load it "click it" it gives me a warning and doesnt go any further
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
switch buttonIndex{
case 0:
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePicker.allowsEditing = true
imagePicker.delegate = self
NSLog("Vælg fra Biblioteket");
break;
case 1:
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = true
imagePicker.delegate = self
NSLog("Vælg Kamera");
break;
default:
NSLog("Default");
break;
}
self.presentViewController(imagePicker, animated: true, completion: nil) // this is the problem
}
the warning is this : Warning: Attempt to present on which is already presenting (null)
how ever if i use this :
self.showDeatilViewController(imagePicker, true) it show up but then i can't dismiss it at all
here's how i thought it would be dismissed
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
If i run this code in viewDidLoad, it works
var imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePickerController.allowsEditing = true
self.presentViewController(imagePickerController, animated: true, completion: { imageP in
})
I figured out that if i write this :
self.presentedViewController?.presentViewController(imagePicker, animated: true, completion: nil)
it shows and closes ?!?!

I've got it working... someone brought to my attention that 8.0 IOS has a bug with opening the photolibery and such from apps..
so i put it in the main queue like this
dispatch_async(dispatch_get_main_queue()){
imagePicker.delegate = self
self.presentViewController(imagePicker, animated: true, completion: nil)
}
and now it works!
little delay but it works

Related

UIImagePickerController back button/cancel button is not visible

I show the phone's gallery with an UIImagePickerController, but there is no back/cancel button on it. How can I add one?
What I have (inside a UIViewController):
func openGallery() {
let picker = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes =
UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
When I run this, I get the gallery, however there is no cancel button at top where it usually is.
Implement UIImagePickerControllerDelegate , UINavigationControllerDelegate and create a variable to assign UIImagePickerController var imagePicker = UIImagePickerController()
and set imagePicker.delegate = self.
func openGallary()
{
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}

I'm getting an error with my code "layer = <CALayer: ADRESS>>'s window is not equal to <UIImagePickerController: ADRESS>'s view's window!"

I am trying to call the takePhoto function and use an imagePickerController() method to try and segue to a different view controller that contains an UIIMageView.
I am trying to pass the photo taken to the new view controller/ class but I receive the error mentioned in the title.
#IBAction func takePhoto(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
self.performSegue(withIdentifier: "postSegue", sender: self)
}

UIImagePickerController slow on opening for the first time, except for when you double tap

I'm getting this really weird behaviour on iOS 9 with swift where I have a tableViewCell that opens an imagePicker when tapped on to take a picture of something, when you tap the cell for the first time it takes like 10 seconds to open the picker, but when you tap it twice it immediately opens...
The initialisation code for the picker is as follows
let certificateImagePicker = UIImagePickerController()
certificateImagePicker.delegate = self
certificateImagePicker.allowsEditing = false
certificateImagePicker.sourceType = .Camera
certificateImagePicker.modalPresentationStyle = .CurrentContext
The code for presenting the picker is presentViewController(certificateImagePicker, animated: false, completion: nil)
I do not now if it related but after opening the picker it show this error message
Snapshotting a view that has not been rendered results in an empty snapshot.
Ensure your view has been rendered at least once before snapshotting or
snapshot after screen updates.
I experienced a similar delay in presenting a UIImagePickerController on the first try. What helped a lot in my case was initialising it when also initializing the parent UIViewController, like so:
class ExampleViewController: UIViewController, UIImagePickerControllerDelegate {
let imagePicker = UIImagePickerController()
func presentImagePicker() {
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
imagePicker.modalPresentationStyle = .currentContext
self.present(imagePicker, animated: false, completion: nil)
}
}
This is only for debug builds, when you run an app connected to Xcode.
private var imagePickerController = UIImagePickerController()
...
#objc func addPhoto() {
imagePickerController.delegate = self
imagePickerController.sourceType = .savedPhotosAlbum
present(imagePickerController, animated: true)
}
...
When I have this implementation it was waiting when the first present was attempted then after changed my variable to lazy variable, then it worked like charm.
private lazy var imagePickerController = UIImagePickerController()

UITabBar dissapear when dismiss UIImagePickerController

In my screenshot below, the structure of my storyboard are UITabBarController -> UINavigationController -> UIViewController. In my UIViewController, I call .Camera using UIImagePickerController in my viewDidLoad. However, when users hit Cancel inside the camera, I dismiss the ViewController, and my tab bar dissapears!
This is my codes for dismiss:
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
EDIT: Added calling of UIImagePickerController
override func viewDidLoad()
{
super.viewDidLoad()
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
try to present the imagePicker Controller 'OverCurrentContext'
imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
or set it in the Storyboard in your UIViewController, hope it can help you.

ImagePicker dismiss tabBar Nav Controller

I have a Tab Bar Controller that presents a NavigationController. In one of the ViewControllers I push on top of this I add the imagePickerController to select a photo. When I cancel or select a picture the tab bar disappears... I tried to look for answers but I could not find one that referenced my specific issue.
Here is my Image Picker Method
#IBAction func attachImageBtnTapped(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
attachImageBtn.backgroundColor = UIColor.greenColor()
dismissViewControllerAnimated(true, completion: nil)
}
How can I avoid this? I tried to see if I had set the tabar to be hidden somewhere like in viewWillAppear but No.
Any ideas, any help would be very much appreciated !
I found the error, I need to present the imagePicker Controller 'OverCurrentContext'.
#IBAction func attachImageBtnTapped(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}

Resources