gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 14:24:29 24 4
gpt4 key购买 nike

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

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

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

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

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)

但是,问题依然存在:

这是水平 View :

enter image description here

最佳答案

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

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

关于ios - AVFoundation 相机预览层与水平方向不匹配(Swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51718108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com