AVFoundation相机预览层与水平方向不匹配(Swift 4)

时间:2018-08-07 02:47:11

标签: ios swift avfoundation swift4

我一直在从互联网和stackoverflow中获取一点点代码,以使一个简单的相机应用程序正常工作。 但是,我注意到,如果我将手机翻转到侧面位置, 我看到两个问题:

1) 相机预览层仅占用半屏

2)并且相机的方向似乎没有变化;它保持固定在垂直位置

我的约束似乎很好,如果我在hortionzal位置查看各种模拟器的UIimageView(相机的预览层),则可以正确缠绕UImage。因此不知道为什么相机预览层仅延伸到屏幕的一半。

enter image description here

(ImagePreview =相机预览层)

至于定向问题,这似乎是编码问题? 我在stackoverflow上查找了一些帖子,但是对于Swift 4我什么也没看到。 不知道这些是否是在Swift 4中执行此操作的简便方法。

iPhone AVFoundation camera orientation

以下是我的相机应用中的一些代码:

import Foundation
import AVFoundation
import UIKit

class CameraSetup{

    var captureSession = AVCaptureSession()
    var frontCam : AVCaptureDevice?
    var backCam : AVCaptureDevice?
    var currentCam: AVCaptureDevice?

    var captureInput: AVCaptureDeviceInput?
    var captureOutput: AVCapturePhotoOutput?


    var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

    func captureDevice()
    {
        let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
        for device in discoverySession.devices{
            if device.position == .front{
                frontCam = device
            }
            else if device.position == .back{
                backCam = device}

            do {
                try backCam?.lockForConfiguration()
                backCam?.focusMode = .autoFocus
                backCam?.exposureMode = .autoExpose
                backCam?.unlockForConfiguration()
            }
            catch let error{
                print(error)}
            }
        }



    func configureCaptureInput(){
        currentCam = backCam!
        do{
            captureInput = try AVCaptureDeviceInput(device: currentCam!)
            if captureSession.canAddInput(captureInput!){
                captureSession.addInput(captureInput!)
            }
        }
        catch let error{
            print(error)
        }
    }




func configureCaptureOutput(){
    captureOutput = AVCapturePhotoOutput()
    captureOutput!.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg])], completionHandler: nil)
    if captureSession.canAddOutput(captureOutput!){
        captureSession.addOutput(captureOutput!)
    }
    captureSession.startRunning()
}

这是PreviewLayer函数:

    func configurePreviewLayer(view: UIView){
        cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resize;
        cameraPreviewLayer?.zPosition = -1;
      view.layer.insertSublayer(cameraPreviewLayer!, at: 0)

        cameraPreviewLayer?.frame = view.bounds


    }

编辑:

根据建议,我将view.bounds移动到了上方一行:

func configurePreviewLayer(view: UIView){
        cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resize;
        cameraPreviewLayer?.zPosition = -1;
        cameraPreviewLayer?.frame = view.bounds
        view.layer.insertSublayer(cameraPreviewLayer!, at: 0)

但是,问题仍然存在:

这是水平视图:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您应该使用UIView而不是UIImageView ..并尝试以下方法:

    cameraPreviewlayer?.videoGravity = AVLayerVideoGravityResize;
    cameraPreviewlayer?.zPosition = -1;
    cameraPreviewlayer?.frame = self.imagePreview.bounds