【问题标题】:AVFoundation camera previewlayer doesn't match horizontal orientation (Swift 4)AVFoundation 相机预览层与水平方向不匹配(Swift 4)
【发布时间】:2018-08-07 02:47:10
【问题描述】:

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

1) 相机预览层只占一半的屏幕

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

我的约束似乎很好,如果我在水平位置查看各种模拟器的 UIimageView(相机的预览层),则 UImage 被正确拉伸。所以不知道为什么相机预览层只拉伸到屏幕的一半。

(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)

但是问题依然存在:

这是水平视图:

【问题讨论】:

    标签: ios swift avfoundation swift4


    【解决方案1】:

    我认为你应该使用 UIView 而不是 UIImageView.. 并试试这个:

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

    【讨论】:

    • @Shazaibqureshi 嗨,所以我做了你建议的更改,但我的问题仍然存在。我对代码进行了一些更改,以使其更具可读性。请参阅对 OP 的编辑。谢谢。
    • "cameraPreviewLayer?.frame = view.bounds" 在插入图层之前移动这一行
    • 嗨,我已经按照你的建议做了,但问题仍然存在 :(。我也上传了一张图片,请查看编辑部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多