UIImagePickerController 允许选择方形图像。但是我怎样才能使它成为 16:9?

Posted

技术标签:

【中文标题】UIImagePickerController 允许选择方形图像。但是我怎样才能使它成为 16:9?【英文标题】:UIImagePickerController allows selecting square images. But how can I make it 16:9? 【发布时间】:2019-04-16 09:06:04 【问题描述】:

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

imagePickerController.allowsEditing = true

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

但是,我想以 16:9 的比例裁剪图像,而不是 1: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)

【讨论】:

以上是关于UIImagePickerController 允许选择方形图像。但是我怎样才能使它成为 16:9?的主要内容,如果未能解决你的问题,请参考以下文章

UIImagePickerController 不是全屏

UIImagePickerController、自定义 UIButton 和 AutoLayout

UIImagePickerController 实况照片

OCMock 模拟 UIImagePickerController

UIImagePickerController 图像没有改变

无法弹出 UIImagePickerController