Camera icon comes in and goes off weirdly - ios

As per title, my Camera system icon comes in and goes off almost immediately. However, when I try to navigate to another screen, it stays there permanently.
Video of how it disappears.
https://vid.me/ggk5
My codes are as below. I've set my view controller as a delegate of UINavigationControllerDelegate. So when the imagepickercontroller is shown, I want to add a camera icon as per the video. However it comes in and goes off.
public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool){
let camera = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(CameraTabTVC.btnOpenCamera))
viewController.navigationItem.leftBarButtonItem = camera
}
func btnOpenCamera(){
self.dismiss(animated: false, completion: nil)
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
picker.allowsEditing = true
present(picker, animated: false, completion: nil)
}

Related

How to have a choose image button with a preview of last taken image

I am building a camera app and would like to have the last taken photo show up as a button to choose a photo. How would I do that?
Right now I am using UIImagePickerController to choose the image, and UIButton with just text to bring up the controller.
#IBAction func choosePhotoBtnPressed(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)
}
I want it to look like the stock camera app.
https://i1.wp.com/9to5mac.com/wp-content/uploads/sites/6/2013/09/screen-shot-2013-09-20-at-9-56-50-am.png?resize=1600%2C1000&quality=82&strip=all

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()

Adding left UIBarButton in UIImagePickerController

I want to add a Camera option on my left bar item inside .PhotoLibrary My codes as shown below, and they don't work.
let picker = UIImagePickerController()
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
picker.navigationItem.leftBarButtonItem = camera
picker.allowsEditing = true
picker.sourceType = .PhotoLibrary
picker.delegate = self
presentViewController(picker, animated: true, completion: nil)
You cannot do this the way you are doing because there is no UINavigationController to UIImagePickerController. In order to do this you must add UIInavigationControllerDlegate method in your class.
Add this method in your viewController
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
print("m in")
viewController.navigationItem.title = "Home"
let camera = UIBarButtonItem(title: "camera", style: .Plain, target:self, action: "btnOpenCamera")
viewController.navigationItem.leftBarButtonItem = camera
The output is like this:
Referred to this post, and interpret from objective C to Swift.
I found a solution to my question. This method will do whatever customisation I need inside a loaded view controller.
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
viewController.navigationItem.leftBarButtonItem = camera
}

Adding System Camera on UIBarButton inside UIImagePickerController

If you are familiar with UIImagePickerController screen for .PhotoLibrary, on the right hand side there is a Cancel button. On the left hand side, I want to add a Camera icon that can be found in XCode library.
My code doesn't work.
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
picker.allowsEditing = true
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: picker, action: Selector("btnOpenCamera"))
picker.navigationItem.leftBarButtonItem = camera
presentViewController(picker, animated: true, completion: nil)
Referred to this post, and interpret from objective C to Swift.
I found a solution to my question. This method will do whatever customisation I need inside a loaded view controller.
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
viewController.navigationItem.leftBarButtonItem = camera
}
It may reset navigationItem to other somewhere else during code execution (for example in viewDidLayoutSubviews).
You can try to inherit from UIImagePickerController and reimplement
func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: , action:self Selector("btnOpenCamera"))//you can handle this with delegate if you want
self.navigationItem.leftBarButtonItem = camera
}
Than just use your inherited class instead of standard UIImagePickerController. Hope it helped.

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.

Resources