ios - AVCaptureVideoPreviewLayer(相机预览)在移动到背景和返回后卡住/卡住

标签 ios camera preview avcapturesession avcapturedevice

想不通这个。 当应用程序处于事件状态时一切正常,而不是有时当我将应用程序移至后台(按主页按钮)然后返回时,预览层卡住/卡住。 我使用 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
    }

最佳答案

对于 future 的读者:这是在您的应用中设置摄像头的正确过程。

首先,感谢上面那些花时间帮助我的人。他们都指引我走向正确的方向。尽管 Bill 对 viewDidLoad 理论有误,但他确实给出了 Apple Project 的解决方案。

这种设置相机的正确方法比我想象的要复杂一点,按照文档进行操作给了我很好的结果。所以对于 Objective-C 程序员:

Objective C cam project

Swift cam project

关于 Andrea 的回答,他确实提出了一些您在创建此类应用程序时应该考虑的重要建议。查看它们 - 它们高度相关(他在 Apple 项目中所说的大部分内容也是如此)。

关于ios - AVCaptureVideoPreviewLayer(相机预览)在移动到背景和返回后卡住/卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30540857/

相关文章:

ios - 如何在 collectionview 中添加 PageviewController

android - 相机在 Galaxy Nexus 中意外崩溃

swiftui - Xcode 11.1 SwiftUI 预览失败

facebook - 如何在新时间轴中显示(我自己的)Flash 播放器的大预览?

ios - Facebook iOS 登录问题 - 错误代码 2

ios - 有没有办法从 Apple Developer Portal 中实际删除设备?

opencv - 使用外部摄像头时,Opencv应用程序崩溃

camera - AVLayerVideoGravityResize 在新设备、iOS 10 上不匹配?

php - 使用 jquery/php/html 更改 img src 和 fileupload 值

ios - 我们可以使用 iOS 检测 SIM 卡是预付费还是后付费?