ios – AVCaptureVideoPreviewLayer(相机预览)在移动到背景和背面后冻结/卡住

前端之家收集整理的这篇文章主要介绍了ios – AVCaptureVideoPreviewLayer(相机预览)在移动到背景和背面后冻结/卡住前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不能把这一个出来.
当应用程序处于活动状态时,一切都可以正常运行,而有时当我将应用程序移动到背景(按住主页按钮)而不是返回时,预览屏幕冻结/卡住.
我使用viewWillAppear和viewDidAppear进行设置.
这是我如何设置一切:
var backCamera = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
  var global_device : AVCaptureDevice!
  var captureSession: AVCaptureSession?

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

captureSession = AVCaptureSession()
        captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
        CorrectPosition = AVCaptureDevicePosition.Back
        for device in backCamera {
            if device.position == AVCaptureDevicePosition.Back {
                global_device = device as! AVCaptureDevice
                CorrectPosition = AVCaptureDevicePosition.Back
                break
            }
        }


        configureCamera()
        var error: NSError?
        var input = AVCaptureDeviceInput(device: global_device,error: &error)


        if error == nil && captureSession!.canAddInput(input) {
            captureSession!.addInput(input)

            stillImageOutput = AVCaptureStillImageOutput()
            stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
            if captureSession!.canAddOutput(stillImageOutput) {
                captureSession!.addOutput(stillImageOutput)

                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                var bounds:CGRect = camera_Preview.layer.bounds
                previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                previewLayer?.bounds = bounds
                previewLayer?.position = CGPointMake(CGRectGetMidX(bounds),CGRectGetMidY(bounds))
                camera_Preview.layer.addSublayer(previewLayer)
                self.view.bringSubviewToFront(camera_Preview)
                self.view.bringSubviewToFront(nan_view)

                captureSession!.startRunning()


            }
        }

ViewDidAppear:

var previewLayer: AVCaptureVideoPreviewLayer?


override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        previewLayer!.frame = camera_Preview.bounds
    }

解决方法

投资回报率,

我认为你的问题是你正在做所有的会话设置,在viewWillAppear中.让我们说,captureSession和previewLayer都被分配并且正常工作.现在,你把应用程序放在后台并带回来.

您将立即尝试创建一个新的captureSession和一个新的previewLayer.我怀疑旧的和新的都在纠结.

在Apple AVCam示例中,他们在viewDidLoad中进行设置.这样做只能做一次.

您应该将所有设置的东西移动到一个方法,然后从viewDidLoad调用方法.

法案

原文链接:/iOS/336409.html

猜你在找的iOS相关文章