@@ -200,19 +200,20 @@ public class UIImageCropper: UIViewController, UIImagePickerControllerDelegate,
200200 return
201201 }
202202 layoutDone = true
203- maskFadeView ( )
204-
203+
205204 if ratio < 1 {
206205 imageWidthConst? . constant = cropView. frame. height / ratio
207206 imageHeightConst? . constant = cropView. frame. height
208207 } else {
209208 imageWidthConst? . constant = cropView. frame. width
210209 imageHeightConst? . constant = cropView. frame. width * ratio
211210 }
212-
211+
213212 let horizontal = NSLayoutConstraint . constraints ( withVisualFormat: " H:|-(<= \( cropView. frame. origin. x) )-[view]-(<= \( cropView. frame. origin. x) )-| " , options: NSLayoutFormatOptions ( ) , metrics: nil , views: [ " view " : imageView] )
214213 let vertical = NSLayoutConstraint . constraints ( withVisualFormat: " V:|-(<= \( cropView. frame. origin. y) )-[view]-(<= \( cropView. frame. origin. y) )-| " , options: NSLayoutFormatOptions ( ) , metrics: nil , views: [ " view " : imageView] )
215214 topView. addConstraints ( horizontal + vertical)
215+
216+ maskFadeView ( )
216217 orgWidth = imageWidthConst!. constant
217218 orgHeight = imageHeightConst!. constant
218219 }
@@ -228,16 +229,34 @@ public class UIImageCropper: UIViewController, UIImagePickerControllerDelegate,
228229
229230 //MARK: - button actions
230231 @objc func cropDone( ) {
231- self . dismiss ( animated: false , completion: {
232+ presenting = false
233+ if picker == nil {
234+ self . dismiss ( animated: false , completion: {
235+ if self . autoClosePicker {
236+ self . picker? . dismiss ( animated: true , completion: nil )
237+ }
238+ self . delegate? . didCropImage ( originalImage: self . image, croppedImage: self . cropImage)
239+ } )
240+ } else {
241+ self . endAppearanceTransition ( )
242+ self . view. removeFromSuperview ( )
243+ self . removeFromParentViewController ( )
232244 if self . autoClosePicker {
233245 self . picker? . dismiss ( animated: true , completion: nil )
234246 }
235247 self . delegate? . didCropImage ( originalImage: self . image, croppedImage: self . cropImage)
236- } )
248+ }
237249 }
238-
250+
239251 @objc func cropCancel( ) {
240- self . dismiss ( animated: true , completion: nil )
252+ presenting = false
253+ if picker == nil {
254+ self . dismiss ( animated: true , completion: nil )
255+ } else {
256+ self . endAppearanceTransition ( )
257+ self . view. removeFromSuperview ( )
258+ self . removeFromParentViewController ( )
259+ }
241260 }
242261
243262 //MARK: - gesture handling
@@ -277,26 +296,51 @@ public class UIImageCropper: UIViewController, UIImagePickerControllerDelegate,
277296
278297 let cropFrame = CGRect ( x: x * imageSize. width, y: y * imageSize. height, width: imageSize. width * width, height: imageSize. height * height)
279298 if let cropCGImage = image. cgImage? . cropping ( to: cropFrame) {
280- let cropImage = UIImage ( cgImage: cropCGImage, scale: 1 , orientation: . up) //(cgImage: cropCGImage,)
299+ let cropImage = UIImage ( cgImage: cropCGImage, scale: 1 , orientation: . up)
281300 return cropImage
282301 }
283302 return nil
284303 }
285304
286305 //MARK: - UIImagePickerControllerDelegates
287306 public func imagePickerControllerDidCancel( _ picker: UIImagePickerController ) {
307+ presenting = false
288308 if delegate? . didCancel ? ( ) == nil {
289309 picker. dismiss ( animated: true , completion: nil )
290310 }
291311 }
292-
312+
313+ var presenting = false
314+
293315 public func imagePickerController( _ picker: UIImagePickerController , didFinishPickingMediaWithInfo info: [ String : Any ] ) {
316+ guard !presenting else {
317+ return
318+ }
294319 guard let image = info [ UIImagePickerControllerOriginalImage] as? UIImage else {
295320 return
296321 }
297322 layoutDone = false
298-
323+ presenting = true
299324 self . image = image. fixOrientation ( )
300- self . picker? . present ( self , animated: true , completion: nil )
325+ self . picker? . view. addSubview ( self . view)
326+ self . view. constraintToFill ( superView: self . picker? . view)
327+ self . picker? . addChildViewController ( self )
328+ self . willMove ( toParentViewController: self . picker)
329+ self . beginAppearanceTransition ( true , animated: false )
330+ }
331+
332+ }
333+
334+ extension UIView {
335+ func constraintToFill( superView view: UIView ? ) {
336+ guard let view = view else {
337+ assertionFailure ( " superview is nil " )
338+ return
339+ }
340+ self . translatesAutoresizingMaskIntoConstraints = false
341+ self . widthAnchor. constraint ( equalTo: view. widthAnchor) . isActive = true
342+ self . heightAnchor. constraint ( equalTo: view. heightAnchor) . isActive = true
343+ self . centerXAnchor. constraint ( equalTo: view. centerXAnchor) . isActive = true
344+ self . centerYAnchor. constraint ( equalTo: view. centerYAnchor) . isActive = true
301345 }
302346}
0 commit comments