iOS相机AVCaptureSession为人像,具有横向尺寸或分辨率

时间:2018-08-03 06:07:55

标签: ios swift avfoundation ios-camera avcapture

我以纵向模式创建了AVCaptureVideoPreviewLayer,我想知道如何在保持纵向模式的同时以16:9比例(横向宽高比)显示它

我尝试过:

1。赋予Camera PreviewLayer 16:9大小,但显示为缩放

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

我想以横向宽高比将相机倾斜到人像或 具有横向宽高比的人像相机

1 个答案:

答案 0 :(得分:0)

计算纵横比,然后相应缩放图像。

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!
}