1

enter image description here

I have an UIImageView taking the whole screen except navigation bar. Besides, I have an UIView with red border on top of UIImageView. When "Done" button is clicked, I want image inside red border being cropped.

I tried hard to use CGImage.cropping(to: rect). But the rect I sent is just frame of UIView with red border, which is different from rect of a cgImage.

How could I send proper 'rect' parameter into cropping(to: rect)method of CGImage instance to crop Image?

Cliffordwh
  • 1,422
  • 1
  • 10
  • 18
Todanley
  • 468
  • 3
  • 12

1 Answers1

2

Place the UIImageView inside a UIView of the same size. Set for the container view's clipsToBounds = YES. When you want to make the image smaller change the frame of the UIView to be the frame of that red frame and change the origin of the frame of the UImageView to be the negative of the origin of the red frame. You can also easily animate this effect if you want by putting it in an animation block

Jon Rose
  • 8,373
  • 1
  • 30
  • 36