Todanley
Todanley

Reputation: 508

How to crop UIImage in ImageView

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?

Upvotes: 1

Views: 1201

Answers (1)

Jon Rose
Jon Rose

Reputation: 8563

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

Upvotes: 2

Related Questions