UIImagePickerController允许选择方形图像。但是我怎样才能做到16:9?

时间:2019-04-16 09:06:05

标签: ios swift uiimagepickercontroller

我设置了UIImagePickerController并添加了代码:

scale_fill_manual(values=x)

这允许用户裁剪图像,但比例仅为1:1。

但是,我想以16:9而不是1:1的比例裁剪图像。我该如何实现?

1 个答案:

答案 0 :(得分:1)

对不起,但是您不能更改默认图像选择器的比例。因此您需要使用其他第三方库。我正在使用下面的库来管理裁剪器,因此您可以使用它。 https://www.cocoacontrols.com/controls/alcameraviewcontroller

这是管理裁切率的代码。

func addImagePickerOnController(callBack: @escaping(_ image:UIImage)-> ()) {
    var croppingParameters: CroppingParameters {
        return CroppingParameters(isEnabled: true, allowResizing: true, allowMoving: true, minimumSize: CGSize(width: 60, height: 60))
    }

    let alertController = UIAlertController(title: nil, message: "Choose Image", preferredStyle: .actionSheet)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
    }
    alertController.addAction(cancelAction)

    let cameraAction = UIAlertAction(title: "Camera", style: .default) { action in
        let cameraVC = CameraViewController(croppingParameters: croppingParameters, allowsLibraryAccess: false, allowsSwapCameraOrientation: true, allowVolumeButtonCapture: true) { [weak self] image, asset in
            if (image != nil){
                callBack(image!)
            }
            self?.dismiss(animated: true, completion: nil)
        }
        self.present(cameraVC, animated: true, completion: nil)
    }

    let galleryAction = UIAlertAction(title: "Gallery", style: .default) { action in
                let libraryViewController = CameraViewController.imagePickerViewController(croppingParameters: croppingParameters) { [weak self] image, asset in
                    if (image != nil){
                        callBack(image!)
                    }
                    self?.dismiss(animated: true, completion: nil)
                }

                self.present(libraryViewController, animated: true, completion: nil)
    }
    alertController.addAction(cameraAction)
    alertController.addAction(galleryAction)
    self.present(alertController, animated: true)
}