如何在控制器中渲染视图?

时间:2015-04-27 01:16:17

标签: ios swift camera viewcontroller

我正在尝试按下按钮弹出相机,但我的视图不会持久,因为我一直收到错误:

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.

我有一个imageView渲染,用于在拍摄后使用ImagePickerController函数放置图片,但我猜它不会被推到视图中。

你有什么想法吗?

可能有用的代码如下: - 基本上,ViewController创建一个UIView capturebutton,当它被点击时,会显示一个Camera视图控制器。

的ViewController

class ViewController: UIViewController, UIScrollViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate{
        let rect1 = CGRectMake(100, 60, 40, 60)
        let captureButton2 = UIView(frame: rect1)
        captureButton2.backgroundColor = UIColor.blueColor()

        captureButton2.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("didTapImageView:")))
        scrollView.addSubview(captureButton2)

    func didTapImageView(tap: UITapGestureRecognizer){
        let captureDetails = storyboard!.instantiateViewControllerWithIdentifier
         ("CameraVC")! as? CameraVC3
        presentViewController(captureDetails!, animated: true, completion: nil)

    }

CameraViewController

class CameraVC3: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIViewControllerTransitioningDelegate{

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            var imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
            imagePicker.mediaTypes = [kUTTypeImage]
            imagePicker.allowsEditing = false
            self.presentViewController(imagePicker, animated: true, completion: nil)
        }
    }
    var photoImageView2 = UIImageView(frame: CGRectMake(40, 120, 200, 200))

    // handles output
    func ImagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
         photoImageView2.image = image
         picker.dismissViewControllerAnimated(true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

知道了!我需要在viewDidLoad() fn

中添加一行
override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(photoImageView)
}