如何缩放我的imagePickerController相机,使其充满整个屏幕?

问题描述 投票:1回答:3

我已经隐藏了相机控件,但这留下了很大的黑色空间。为了解决这个问题,我想缩放我的相机,使其覆盖整个屏幕。我不知道为什么我无法做到这一点。这是我的代码。干杯!

imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.showsCameraControls = false
imagePicker.cameraOverlayView = overlayView
self.presentViewController(imagePicker, animated: false, completion: nil)
ios swift camera uiimagepickercontroller cgaffinetransform
3个回答
0
投票

我相信相机的纵横比为4/3,因此您必须应用变换比例才能使相机全屏显示。

let screenSize = UIScreen.mainScreen().bounds.size
let aspectRatio:CGFloat = 4/3
let scale = screenSize.height/screenSize.width * aspectRatio
self.imagePikerViewController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);

0
投票

由于摄像头的纵横比为4:3,因此您无法拥有全屏摄像头。

但是,您可以拉伸图像以填满整个屏幕。但是我个人不建议这样做,因为图像可能看起来不正确。尽管可以使用AV Foundation,但特别是AVCaptureVideoPreviewLayer可以实现相同的效果。你可以参考这些链接1.https://developer.apple.com/library/prerelease/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html2.https://developer.apple.com/av-foundation/欲获得更多信息。希望能帮助到你...快乐编码..:)


0
投票

这里是可在iOS 13.x上运行的解决方案,并已通过iPhone 6+和iPhone 11 Pro的测试。

    let screenSize = UIScreen.main.bounds.size
    let cameraAspectRatio = CGFloat(4.0 / 3.0)
    let cameraImageHeight = screenSize.width * cameraAspectRatio
    let offsetToCenter = screenSize.height - cameraImageHeight
    let scale = (screenSize.height + offsetToCenter) / cameraImageHeight

    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        myPickerController.delegate = self
        myPickerController.sourceType = .camera
        myPickerController.showsCameraControls = false
        myPickerController.allowsEditing = false
        myPickerController.cameraViewTransform = CGAffineTransform(scaleX: scale, y: scale)
        myPickerController.cameraOverlayView = CameraOverlay(frame: vc.view.frame, delegate: self)
        currentVC.present(myPickerController, animated: true)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.