OGeek|极客世界-中国程序员成长平台

标题: iOS 相机 AVCaptureSession 在纵向与横向大小或分辨率 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:25
标题: iOS 相机 AVCaptureSession 在纵向与横向大小或分辨率

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

我试过了:

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

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

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



Best Answer-推荐答案


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

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4