2017-02-16 47 views
0

返回編輯的圖像,所以我有像這樣設置的選擇器控制器:始終從的UIImagePickerController

func displayImagePickerButtonTapped() { 

    let myPickerController = UIImagePickerController() 
    myPickerController.delegate = self 
    myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary 
    myPickerController.allowsEditing = true 

    self.present(myPickerController, animated: true, completion: nil) 
} 

現在myPickerController.allowsEditing = true允許編輯。但是,用戶仍然可以返回原始圖像而不是編輯圖像,因爲編輯不是必須的,而是在這種情況下的奢侈品。

我該如何讓用戶只選擇一個編輯過的圖像,它總是每次都被裁剪成正方形的形狀?

+1

您無法強制用戶編輯圖像。至少不能用'UIImagePickerController'。 – rmaddy

回答

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函數,您可以根據需要獲取裁剪後的圖像以使用它。

+0

是的,我做到了,但是當用戶裁剪圖像時,它仍然允許在編輯過程中圖像周圍出現黑色的間距。編輯圖像的返回不是問題。 – luke

+0

@luke讓我編輯我的答案 –

+0

@luke現在檢查這對你是否有用 –