ios - 为什么我的预览图层没有覆盖整个 View ?

标签 ios iphone swift avfoundation

我正在使用 AVFoundation 相机制作 iPhone 应用程序,但相机无法正确缩放。

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

为什么我的预览图层没有延伸到整个 View ?是我输入错误还是只是忘记了需要输入的内容?谢谢!

图片: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()
    }
}

最佳答案

我复制您的代码并在 iOS 10.1.1iPhone6XCode 8.2.1 上运行,它可以工作。 Screen shot of your workable code

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

关于ios - 为什么我的预览图层没有覆盖整个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45405309/

相关文章:

ios - 我将如何保存特定日期的文本字段?

使用 AVFoundation 和 Swift 的 iOS 多 channel 音频

ios - Iphone 如何使上下文背景透明?

iphone - UIWebView 的内容高度在使用 mathjax 时返回错误值

ios - self Controller nil webview

swift - Swift编译器错误: During the test the app in the iphone device

ios - React-Native:如何为动态链接的一个构建添加多个 google-service-info plist 文件?

ios - 从 NSCalendar 组件获取小时、分钟、秒? ( swift )

ios - unregisterForRemoteNotifications 不适用于 iOS8 - 推送通知

ios - Swift iOS 无法识别 GCM 库导入?