简体   繁体   中英

bringing up new view is super slow

I am bringing up a new view and it gets slower and slower the more times I display it. Its a custom camera view controller and each time I take a video or photo and post it then open the camera again it is super slow.

The methods that seem to be taking forever and slow down the more times the view is added are self.addChildViewController and self.cameraView.insertSubview

here is the relevant code:

NSOperationQueue.mainQueue().addOperationWithBlock() {
    let controller = self.storyboard?.instantiateViewControllerWithIdentifier("CameraCaptureViewController") as! CameraCaptureViewController
                        println("after instantiate viewcontrolller")
                        controller.view.setTranslatesAutoresizingMaskIntoConstraints(false)
                        controller.captureDelegate = self
                        controller.mediaType = typeString

                        controller.customCaptureButton = self.mediaButton

                        self.addChildViewController(controller)
                        println("after addChildViewController")
                        controller.view.backgroundColor = UIColor(white: 1.0, alpha: 0.15)
                        self.cameraView.insertSubview(controller.view, atIndex:1)
                        println("after insertSubview")
                        //Swapping controls to the main view button.

                        self.cameraView.userInteractionEnabled = true

                        self.cameraView.layoutIfNeeded()
                        controller.didMoveToParentViewController(self)
                        println("after didMoveToParentViewController")
                        self.captureController = controller
                        println("in op queue end")
}

also when this is dismissed this is the code that runs:

captureController.willMoveToParentViewController(nil)
captureController.view.removeFromSuperview()
captureController.removeFromParentViewController()

cameraView.userInteractionEnabled = false
for view in self.cameraView.subviews {
    view.removeFromSuperview()
}
self.captureController = nil

How do I debug this further? What could be slowing those methods down?

As always when something is slow, you should suspect that you are calling your code on a background thread . (Your own println("in op queue end") suggests that this is so.) The slowness is merely diagnostic of a much deeper issue. It is illegal to touch the interface in any way, shape, or form on a background thread - and that is what you are doing when you say self.cameraView.insertSubview .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM