UIImagePickerController允许选择方形图像.但是我怎么能做到16:9



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

imagePickerController.allowsEditing = true

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

但是,我想以 16:9 而不是 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)
}

最新更新