2017-02-16 2 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は編集することができます。しかし、編集は必須ではなく、この場合は贅沢なので、ユーザーは編集済みの画像ではなく元の画像を返すことができます。

編集した画像のみを選択できるようにするにはどうすればいいですか?毎回正方形の形に切り捨てられますか?ユーザーが写真を撮るか、フォトライブラリからいずれかを選択する際

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) 
} 

ので、イメージピッカーを閉じて:

+1

ユーザーが強制的に画像を編集することはできません。少なくとも 'UIImagePickerController'ではそうではありません。 – rmaddy

答えて

2

は私がTOCropViewController

だから、私のdidFinishPickingMediaに私が持っているというライブラリを使用しますトリミングビューコントローラーが開きます。そこでは、ユーザーは特定の比率で切り抜くことができます。 cropViewController didCropTo関数を使用すると、クロップされたイメージが必要に応じて使用されます。

+0

そうだが、ユーザーが画像を切り抜いているときには、編集中に画像の周りに黒いスペーシングが残る。編集した画像の復帰は問題ではありません。 – luke

+0

@luke私の回答を編集させてください –

+0

@lukeこれがあなたに役立つかどうかチェックしてください –

関連する問題