2015-09-09 102 views
0

你好,我有一个工作代码AVCaptureVideoPreviewLayer ..这是一个部分条形码阅读器:AVCaptureVideoPreviewLayer黑屏

let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

    do { 
     let input = try AVCaptureDeviceInput(device: captureDevice) as AVCaptureDeviceInput 
     session.addInput(input) 
     print("input done..") 
    } catch let error as NSError { 
     print(error) 
    } 

    let output = AVCaptureMetadataOutput() 
    output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 
    session.addOutput(output) 
    output.metadataObjectTypes = output.availableMetadataObjectTypes 

    previewLayer = AVCaptureVideoPreviewLayer(session: session) as AVCaptureVideoPreviewLayer 
    previewLayer.frame = self.view.bounds 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
    self.view.layer.addSublayer(previewLayer) 
    self.view.bringSubviewToFront(self.highlightView) 
    session.startRunning() 

它的启动和运行也,有没有任何错误messeage和我在我的旧iPhone使用我也可以看到相机的图片。但2天前我的iPhone已被替换,我没有改变代码中的任何东西。现在的应用程序开始,但我只能看到黑屏。

有没有人知道可能会导致这种情况?

谢谢!

+0

在设置>隐私 - >相机检查设备,并确保您的应用已启用访问摄像机 –

+0

不,它没有启用..但我怎样才能访问使用相机?这是相同的代码..我的旧设备并没有要求我既不启用访问相机。 – solarenqu

+0

所以没有一个弹出窗口,我可以启用。 – solarenqu

回答

0

从iOS 8的需要请求许可,您可以做这样的事情

if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) { 
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { 

     if (granted) { 
      // Got the permission 
     } else { 
      // Permission has been denied. 
     } 
    }]; 
} else { 
    // below ios 6 
} 

您可以执行swifth

if AVCaptureDevice.respondsToSelector(Selector("requestAccessForMediaType")) 
{ 
    AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (grandted:Bool) -> Void in 
      //Check if granted and if its true do camera work else permission denied 
    }) 
} 
else 
{ 
// Below iOS 6 
} 
+0

当然,但在我的其他设备上它为什么工作?它是ios9测试版。我没有改变代码中的任何东西。 – solarenqu

+0

您的其他设备是否正在运行iOS9测试版?如果是这种情况,那么你必须要求许可, –

+0

@solarenqu有没有可能你忘记了让你的应用程序访问了以前的设备上的相机? – Gliderman

0

此权限的方法是我用来测试的代码出:

override func viewDidLoad() { 
    super.viewDidLoad() 
    var session = AVCaptureSession.new() 
    session.sessionPreset = AVCaptureSessionPresetMedium 

    let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

    let input = AVCaptureDeviceInput(device: captureDevice, error: nil) as AVCaptureDeviceInput 
    session.addInput(input) 
    print("input done..") 

    var previewLayer = AVCaptureVideoPreviewLayer(session: session) as AVCaptureVideoPreviewLayer 
    previewLayer.frame = self.view.bounds 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
    self.view.layer.addSublayer(previewLayer) 
    session.startRunning() 
} 

我看来问题是你没有通过NSError需要AVCaptureDeviceInput。 Xcode不确定try声明也试图做什么。我现在只是简单地将nil传入构造函数中,但你想要确保处理它。我还删除了设置输出的部分,因为这与我的测试无关。随意添加回来。

我在运行iOS 7.1的iPhone 4上测试了这个参考。

+0

谢谢..但在iOS 9此代码不工作.. – solarenqu

+0

无法找到类型'AVCaptureDeviceInput',接受类型的参数列表'(device:AVCaptureDevice !,error:nil)' 这是第一个错误.. :) – solarenqu

+0

@solarenqu你有AVFoundation.framework添加到你的项目吗?随着你的代码顶部的'import AVFoundation'? – Gliderman

-1

我将整个代码复制到一个新的项目中,并且在重新部署之后,它向摄像机请求权限.. :)我不知道这是什么。感谢您的回答。