始终从UIImagePickerController返回已编辑的图像

时间:2017-02-16 14:57:04

标签: ios swift

所以我有一个选择控制器,其设置如下:

    <testsuites>
        <testsuite name="Feature Tests">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

现在func displayImagePickerButtonTapped() { let myPickerController = UIImagePickerController() myPickerController.delegate = self myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary myPickerController.allowsEditing = true self.present(myPickerController, animated: true, completion: nil) } 允许编辑。但是,用户仍然可以返回原始图像而不是编辑图像,因为编辑不是必须的,而是在这种情况下的奢侈品。

如何让用户只选择一个经过编辑的图像,每次都会将其裁剪成正方形的形状?

1 个答案:

答案 0 :(得分:2)

我使用名为TOCropViewController

的库

所以,在我的didFinishPickingMedia中我有:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
        dismiss(animated: true, completion: nil)
        let cropVC = TOCropViewController(image: pickedImage)
        cropVC.delegate = self
        cropVC.aspectRatioPickerButtonHidden = true
        cropVC.aspectRatioPreset = .presetSquare //Here you can set the ratio as 4.3, 4.2, square, etc
        cropVC.aspectRatioLockEnabled = true //Here you lock the ratio
        cropVC.resetAspectRatioEnabled = false
        self.present(cropVC, animated: true, completion: nil)
    }
}

func cropViewController(_ cropViewController: TOCropViewController, didCropTo image: UIImage, with cropRect: CGRect, angle: Int) {
    self.yourImage.image = image
    cropViewController.dismiss(animated: true, completion: nil)
}

因此,当用户拍照或从照片库中选择一张照片时,图像选择器将关闭,裁剪视图控制器将打开。在那里,用户可以以特定比率进行裁剪。使用cropViewController didCropTo函数,您可以根据需要使用裁剪后的图像。