简体   繁体   English

如何在ImageView中裁剪UIImage

[英]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.

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