简体   繁体   中英

How to crop UIImage in ImageView

在此处输入图片说明

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM