UIImagePickerController允许选择方形图像。但我怎么能以16:9的比例成功呢?

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

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

imagePickerController.allowsEditing = true

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

但是,我想以16:9而不是1:1的比例裁剪图像。我怎样才能做到这一点?

ios swift uiimagepickercontroller
1个回答
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)
}
© www.soinside.com 2019 - 2024. All rights reserved.