我现在有这个:
@IBAction func changeCamera(sender: AnyObject) { camera = true}overrIDe func vIEwWillAppear(animated: Bool) { captureSession = AVCaptureSession() captureSession!.sessionPreset = AVCaptureSessionPresetPhoto var captureDevice:AVCaptureDevice! = nil //var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVIDeo) if (camera == false){ let vIDeoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVIDeo) for device in vIDeoDevices{ let device = device as AVCaptureDevice if device.position == AVCaptureDeviceposition.Front { captureDevice = device break } } } else { var captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVIDeo) } var error: NSError? var input = AVCaptureDeviceinput(device: captureDevice,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) prevIEwLayer!.vIDeoGravity = AVLayerVIDeoGravityResizeAspect prevIEwLayer!.connection?.vIDeoOrIEntation = AVCaptureVIDeoOrIEntation.Portrait prevIEwVIEw.layer.addSublayer(prevIEwLayer) captureSession!.startRunning() } }}解决方法 问题出在您设置相机源的位置.
您在vIEwDIDAppear中设置它,只有当视图出现在设备上时才会调用它.这是每当您从另一个视图控制器导航到该视图控制器或关闭由此视图控制器呈现的呈现视图控制器时.
我的建议是将摄像机选择代码移动到它自己的函数中,该函数由vIEwDIDLoad调用,也可以在调用changeCamera *** 作时调用.
@IBAction func changeCamera(sender: AnObject?) { camera = !camera reloadCamera()}func vIEwDIDAppear(animated: Bool) { // normal code reloadCamera()}func reloadCamera() { // camera loading code captureSession = AVCaptureSession() captureSession!.sessionPreset = AVCaptureSessionPresetPhoto var captureDevice:AVCaptureDevice! = nil // var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVIDeo) if (camera == false) { let vIDeoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVIDeo) for device in vIDeoDevices{ let device = device as AVCaptureDevice if device.position == AVCaptureDeviceposition.Front { captureDevice = device break } } } else { var captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVIDeo) } var error: NSError? var input = AVCaptureDeviceinput(device: captureDevice,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) prevIEwLayer!.vIDeoGravity = AVLayerVIDeoGravityResizeAspect prevIEwLayer!.connection?.vIDeoOrIEntation = AVCaptureVIDeoOrIEntation.Portrait prevIEwVIEw.layer.addSublayer(prevIEwLayer) captureSession!.startRunning() }}
此外,另一项改进是使用自定义枚举来存储当前使用的相机而不是布尔值.这意味着您可以稍后添加它,如果有第三个相机.这看起来像:
enum CameraType { case front case back}var camera = CameraType.back
我希望这有帮助,对于省略完整的代码示例表示歉意,目前在iPad上,但是当我到达计算机时我会更新.
更新
在更换相机之前,请确保从视图中删除上一个预览图层.
func reloadCamera() { captureSession?.stopRunning() prevIEwLayer?.removeFromSuperlayer() // The rest of the camera loading code...
这应该可以解决您的相机冻结问题.
总结以上是内存溢出为你收集整理的ios – 如何在Swift中创建一个更改摄像机视图的按钮?全部内容,希望文章能够帮你解决ios – 如何在Swift中创建一个更改摄像机视图的按钮?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)