【问题标题】:Why doesn't my preview layer stretch over the entire view?为什么我的预览层没有延伸到整个视图?
【发布时间】:2017-07-30 22:24:35
【问题描述】:

我正在使用 AVFoundation 摄像头制作 iPhone 应用,但摄像头无法正确缩放。

我认为我已经做了很多工作以使其大小相同,我将视频重力更改为 ResizeAspectFill 并将 previewlayer.frame.size 更改为 self.layer.frame.size。

为什么我的预览层没有覆盖整个视图?是我打错了还是忘记了我需要输入?谢谢!

图片:http://imgur.com/O713SoE

代码:

import AVFoundation
import UIKit
import QuartzCore

class View1: UIViewController {

    let captureSession = AVCaptureSession()
    var previewLayer: CALayer!
    var captureDevice: AVCaptureDevice!

    @IBOutlet weak var photoButton: UIButton!
    @IBOutlet weak var cameraView:  UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        photoButton.layer.zPosition = 1
    }

    @IBAction func photoButtonpressed(_ sender: UIButton) {
        let button = sender as UIButton
        if (button.tag == 1){
            print("Photobutton clicked")        
        }
    }

    func prepareCamera(){

        captureSession.sessionPreset = AVCaptureSessionPreset1920x1080

        if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera],
                                                                               mediaType: AVMediaTypeVideo,
                                                                               position: .back).devices {

            captureDevice = availableDevices.first

            beginSession()  
        }
    }

    func beginSession(){
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
            captureSession.addInput(captureDeviceInput)       
        } catch {  
            print(error.localizedDescription)
        }

        if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession){

            self.previewLayer = previewLayer

            self.view.layer.addSublayer(self.previewLayer)

            self.previewLayer.frame = self.view.layer.frame
            self.previewLayer.bounds = self.view.bounds
            self.previewLayer.contentsGravity = AVLayerVideoGravityResizeAspectFill

            captureSession.startRunning()

            let dataOutput = AVCaptureVideoDataOutput()

            dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value: kCVPixelFormatType_32BGRA)]

            dataOutput.alwaysDiscardsLateVideoFrames = true

            if captureSession.canAddOutput(dataOutput) {
                captureSession.addOutput(dataOutput)
                captureSession.commitConfiguration()        
            }
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)    
        prepareCamera()    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

【问题讨论】:

  • 您需要覆盖viewDidLayoutSubviews 并执行:self.previewLayer.frame = self.view.bounds,因为图层不受约束。他们采用父级的边界,但是当父级布局时它不会调整大小..所以你必须像这样手动完成。 self.previewLayer.frame = self.view.layer.frame self.previewLayer.bounds = self.view.bounds 是错误的。设置框架后不要更改边界。仅设置框架就足够了,因为框架实际上是“位置”加上“边界”。
  • 这就是视图控制器中的所有代码吗?你在viewDidLoad 之前拥有的这个cameraView 出口是什么,但从不在你的代码中使用?它来自故事板,我想……有限制吗?

标签: ios iphone swift avfoundation


【解决方案1】:

我复制您的代码并在iOS 10.1.1iPhone6XCode 8.2.1 上运行它可以工作。

你如何加载View1?以编程方式?在故事板中启动? View1view 可能与您的设备屏幕大小不同。

【讨论】:

  • 太奇怪了,代码是从我制作的另一个项目中复制的(在哪里工作),当我复制并重做所有内容时,我得到的项目在哪里相同。
  • 你如何显示你的View1
  • 我不太确定你的意思,但我使用滚动视图,view1 是 XIB 文件。
  • 问题在于你如何发起View并显示它。
  • 好的,我该如何解决?在我创建一个新项目并将其粘贴之前,它在我的设备上运行
猜你喜欢
  • 2019-06-10
  • 2013-06-08
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2011-01-10
  • 2021-02-25
相关资源
最近更新 更多