2

我在使用 PBJVision 时遇到了一些问题。

我将它集成到一个 Swift 项目中,当我尝试设置预览层的框架时,它会导致框架不一致: img_1678

我有这个代码:

var _previewLayer: AVCaptureVideoPreviewLayer = PBJVision.sharedInstance().previewLayer
@IBOutlet weak var previewView: UIView! // this is the white view in the back

let bounds = self.previewView.layer.bounds
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
_previewLayer.bounds = bounds
_previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))
previewView.layer.addSublayer(_previewLayer)
4

2 回答 2

4

您是否尝试过在布局子视图_previewLayer时调整框架的大小?previewView

您的子类UIView,为您previewView设置一个属性AVCaptureVideoPreviewLayer,并将其框架设置为与您的视图边界相匹配:

class PreviewView: UIView {

    let previewLayer: AVCaptureVideoPreviewLayer?

    override func layoutSubviews() {
        self.previewLayer?.frame = self.bounds;
    }
}
于 2016-02-03T04:24:21.450 回答
0

我遇到过同样的问题。

我尝试了@rkyr 推荐(在viewDidLayoutSubviews方法中设置框架),但没有成功。我也想在不添加子类的情况下修复它。

我能够通过 2 个更改来修复它: - 确保 previewLayer 被定义为AVCaptureVideoPreviewLayer(subclass of CALayer) 而不是CALayer,这就是我的情况

var previewLayer: AVCaptureVideoPreviewLayer? // :CALayer
  • 确保设置了图层的框架,并在设置了图层会话的所有设置后将其添加到视图中:此代码有效:

    captureSession = AVCaptureSession()
    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
    do {
        let input = try AVCaptureDeviceInput(device: captureDevice!)
        captureSession?.addInput(input)
    } catch {
        onErrorCloseAndReturn(error: error)
    }
    let dataOutput = AVCaptureVideoDataOutput()
    dataOutput.videoSettings = [((kCVPixelBufferPixelFormatTypeKey as NSString) as String): NSNumber(value: kCVPixelFormatType_32BGRA)]
    dataOutput.alwaysDiscardsLateVideoFrames = true
    if captureSession!.canAddOutput(dataOutput) {
        captureSession?.addOutput(dataOutput)
    }
    captureSession?.commitConfiguration()
    let queue = DispatchQueue(label: "captureQueue")
    dataOutput.setSampleBufferDelegate(self, queue: queue)
    captureSession?.startRunning()
    
    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
    previewLayer?.frame = self.view.layer.bounds
    self.view.layer.addSublayer(previewLayer!)
    

我锄头它有帮助... e

于 2018-05-31T16:15:31.323 回答