我在纵向模式下创建了 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/