[英]How to crop UIImage in ImageView
I have an UIImageView
taking the whole screen except navigation bar. 我有一个
UIImageView
占据了整个屏幕,除了导航栏。 Besides, I have an UIView
with red border on top of UIImageView
. 此外,我在
UIImageView
顶部有一个带有红色边框的UIView
。 When "Done" button is clicked, I want image inside red border being cropped. 单击“完成”按钮时,我希望裁切红色边框内的图像。
I tried hard to use CGImage.cropping(to: rect)
. 我努力使用
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
. 但是我发送的矩形只是带有红色边框的
UIView
框架,与cgImage
矩形不同。
How could I send proper 'rect' parameter into cropping(to: rect)method of CGImage
instance to crop Image? 如何将适当的“ rect”参数发送到
CGImage
实例的裁剪(裁剪)方法中以裁剪图像?
Place the UIImageView
inside a UIView
of the same size. 将
UIImageView
放置在相同大小的UIView
中。 Set for the container view's clipsToBounds = YES
. 为容器视图的
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. 当您想缩小图像时,将
UIView
的框架更改为该红色框架的框架,并将UImageView
的框架的原点更改为该红色框架的负片。 You can also easily animate this effect if you want by putting it in an animation block 如果需要,还可以通过将其放入动画块中来轻松制作该动画
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.