具有横向大小或分辨率的纵向 iOS 相机 AVCaptureSession

标签 ios swift avfoundation ios-camera avcapture

我在纵向模式下创建了 AVCaptureVideoPreviewLayer,我想知道如何在纵向模式下以 16:9 比例(横向纵横比)显示它

我试过:

1.给相机 PreviewLayer 16:9 大小但它显示为缩放

2.我尝试使用 videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.landscape 分辨率是完美的但它显示为旋转图像(横向相机)

我想将相机倾斜成横向纵横比的纵向或 具有横向纵横比的人像相机

最佳答案

计算宽高比然后相应地缩放图像..

func imageWithImage (sourceImage:UIImage, scaledToWidth: CGFloat) -> UIImage {
    let oldWidth = sourceImage.size.width
    let scaleFactor = scaledToWidth / oldWidth

    let newHeight = sourceImage.size.height * scaleFactor
    let newWidth = oldWidth * scaleFactor

    UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight))
    sourceImage.draw(in: CGRect(x:0, y:0, width:newWidth, height:newHeight))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}

关于具有横向大小或分辨率的纵向 iOS 相机 AVCaptureSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666255/

相关文章:

ios - UIWebView 加载属性永远不会改变

ios - 我如何在第 2 代 SKSpriteNode 之间等待?

ios - 无法观察 @“status” 键的 AVPlayerItem

ios - 如何将 YUVJ420P 中的 FFMPEG AVFrame 转换为 AVFoundation cVPixelBufferRef?

ios - 使用 AVMutableVideoComposition 时 AVMutableComposition 卡住的奇怪行为

ios - 在本地通知中点击后无法在特定 View Controller 中打开应用程序

iphone - UIView 子类中使用的 CAShapeLayer 不起作用

ios - loadNibNamed() 失败 : Unknown class <mangled Swift identifier> in Interface Builder file

ios - UINavigationController 作为 MSMessagesAppViewController 的 subview

Swift 4.2 : [Swift. _EmptyArrayStorage _getValue:forType:]: 无法识别的选择器