AVCaptureVideoPreviewLayer(相机预览)在移动到背景和返回后冻结/卡住 [英] AVCaptureVideoPreviewLayer (camera preview) freezes/stuck after moving to background and back
问题描述
想不出来这个.当应用程序处于活动状态时一切正常,而 有时 当我将应用程序移动到后台(按下主页按钮)而不是返回时,预览层会冻结/卡住.我使用 viewWillAppear 和 viewDidAppear 进行设置.这就是我设置一切的方式:
Cant figure this one out. Everything works fine when the app is active, and than sometimes when i move the app to the background(pressing the home button) and than going back, the previewlayer freezes/stuck. Im using viewWillAppear and viewDidAppear for the set up. This is how i set everything up :
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 :
ViewDidAppear :
var previewLayer: AVCaptureVideoPreviewLayer?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
previewLayer!.frame = camera_Preview.bounds
}
推荐答案
对于未来的读者:这是在您的应用程序中设置相机的正确过程.
For future readers: this is the correct process to setting up camera inside your app.
首先,感谢上面那些花时间帮助我的人.他们都将我引向正确的方向.虽然 Bill 对 viewDidLoad
理论有误解,但他确实给出了 Apple Project 的解决方案.
First of all, thanks for the folks above that took their time and tried help me. They both direct me into the correct direction. Although Bill was mistaken about the viewDidLoad
theory, he did gave the solution Apple Project.
这个设置相机 - 正确的方法 - 比我想象的要复杂一些,按照文档给了我很好的结果.所以对于 Objective-C 的程序员来说:
This setting up camera - the correct way - is little more complicated than I thought, follow the documentation gave me excellent results. So for the Objective-C coders:
关于 Andrea 的回答,他确实说过一些您在创建此类应用程序时应该考虑的重要建议.看看它们——它们是高度相关的(他在 Apple 项目中所说的大部分内容也是如此).
About Andrea answer, he did said some great pointers you should consider when you are creating this kind of app. Check them out - they are highly relevant (most of the things he said inside the Apple project as well).
这篇关于AVCaptureVideoPreviewLayer(相机预览)在移动到背景和返回后冻结/卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!