5

this question ask again but i dont find for ios 10

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
 {
            self.imagePicker.delegate = self
            self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            self.imagePicker.allowsEditing = false
            self.imagePicker.cameraCaptureMode = .photo
            //self.imagePicker.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
            self.present(self.imagePicker, animated: true, completion: nil)
            self.imagePicked.isUserInteractionEnabled = true

 }
else
 {
            print("No Camera")
 }

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.

when i rotate the camera and take a shot than this error occurs.

Community
  • 1
  • 1
seggy
  • 1,176
  • 2
  • 18
  • 38

3 Answers3

6

Self Solution Working for me like charm :-) hope its helpful for all

DispatchQueue.global(qos: .userInitiated).async
{
     self.present(self.imagePicker, animated: true, completion: nil)

}
seggy
  • 1,176
  • 2
  • 18
  • 38
6

I got the error

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes...

Using DispatchQueue.main.async instead works for me.

Lawliet
  • 3,438
  • 2
  • 17
  • 28
1

This behavior is not limited to UIImagePickerController. Below is an example of a UIViewController which presents another UIViewController modally. In the second UIViewController, Safari is launched to present a URL, thus triggering the same error message, "Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES."

I haven't yet found any way of suppressing the message, but in my app it does no harm. I think what's going on here is that some Apple code is taking a snapshot of the app's view hierarchy, but the keyboard (which is owned by a separate UIWIndow) has not been rendered before the snapshot is taken.

/* Generates the error message:

 Cannot snapshot view (<UIKeyboardImpl: 0x7f82ded12ea0; frame = (0 0; 414 271); layer = <CALayer: 0x610000035e20>>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.

 ... after the "Now Tap Me, For Glory!" label is clicked
*/

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let input = UITextField()
        input.placeholder = "Tap here first -- to bring up keyboard"
        input.frame = CGRect(x: 10, y: 50, width: 300, height: 20)
        view.addSubview(input)

        let button = UIButton()
        button.setTitleColor(UIColor.blue, for: .normal)
        button.setTitle("Then tap here", for: .normal)
        button.addTarget(self,
                         action: #selector(buttonPushed),
                         for: .touchUpInside)
        button.frame = CGRect(x: 10, y: 80, width: 200, height: 20)
        view.addSubview(button)
    }

    func buttonPushed() {
        let modalVC = ModalViewController()
        present(modalVC, animated: true, completion: nil)
    }
}

class ModalViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        let label = UILabel()
        label.text = "Now Tap Me, For Glory!"
        label.frame = CGRect(x: 10, y: 50, width: 300, height: 20)
        view.addSubview(label)
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(labelTapped)))
    }

    func labelTapped() {
        UIApplication.shared.open(URL(string: "http://planetbeagle.com")!, options: [:], completionHandler: { _ in
                self.dismiss(animated: true, completion: nil) })
    }
}
Greg Anderson
  • 540
  • 4
  • 14
  • 1
    Not directly related to the question, but if you're on iOS you might want to consider "tap" instead of "click". – buildsucceeded May 09 '17 at 12:24
  • 2
    @buildsucceeded Hehe, aren't all iOS developers running this in the (clickable) simulator? ;-) Nonetheless, I have bowed to your applied peer pressure! – Greg Anderson May 09 '17 at 22:54