如何在椭圆形或圆形上裁剪 UIImage?
请提供有关如何在椭圆形或圆形上裁剪 UIImage
的想法。请分享您的想法。
Please give ideas for how to crop UIImage
on oval shape or circle shape. Please share your ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
通过增加半径,它将变得更圆。
只要图像是正方形,以宽度的一半作为角半径就可以得到完美的圆:
还需要添加
Swift 4.2
by increasing radius it will become more round-able.
As long as the image is a square, you can get a perfect circle by taking half the width as the corner radius:
You also need to add
Swift 4.2
几周前我开始研究这个问题。我尝试了这里的所有建议,但没有一个效果很好。秉承 RTFM 的伟大传统,我阅读了 Apple 的文档 Quartz 2D 编程并提出了这个。请尝试一下并让我知道你的进展如何。
可以相当容易地更改代码以裁剪为椭圆形或由路径定义的任何其他形状。
确保您的项目中包含 Quartz 2D。
在您的 UIView 类中包含以下代码,将“monk2.png”替换为您自己的图像名称。
I started looking into this a couple of weeks back. I tried all the suggestions here, none of which worked well. In the great tradition of RTFM I went and read Apple's documentation on Quartz 2D Programming and came up with this. Please try it out and let me know how you go.
The code could be fairly easily altered to crop to an elipse, or any other shape defined by a path.
Make sure you include Quartz 2D in your project.
Include the following code in your UIView class replacing "monk2.png" with your own image name.
将 imageView 做成椭圆形并不困难。
您可以执行以下操作
如果传递给 bezierPathWithOvalInRect 的矩形是正方形,则图像将被裁剪为圆形。
Swift 代码
To have imageView in oval shape is not difficult.
You can do the following
If the rect passed to bezierPathWithOvalInRect is Square the image will be cropped to circle.
Swift Code
制作 RoundShape 图像
第 1 步:在 .h 文件中
第 2 步:在 .m 文件中
OR 对于 Swift
To make a RoundShape Image
Step1: in .h file
Step2: in .m file
OR For Swift
经过长时间的搜索,我找到了圈出图像的正确方法
从 URL 下载支持存档文件 http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
以下行用于调整图像大小并转换为带半径的圆形
After a long search I found the correct way to circle the image
Download the Support archive file from URL http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
Following lines used to resize the image and convert in to round with radius
环球银行金融电信协会
SWIFT
如果您只需要一个完美的圆形,更改 UIImageView 的形状可能会有所帮助。
只需将 QuartzCore 框架添加到您的项目中,并在显示 imageView 之前将这些代码行添加到生命周期中的某个位置:
If you only need a perfect circle, changing the shape of the UIImageView could help.
Simply add the QuartzCore framework to your project and add these lines of code somewhere in the lifecycle before the imageView is displayed:
查看
CGImageCreateWithMask
。创建椭圆形蒙版,然后将其应用到图像上。Check out
CGImageCreateWithMask
. Create a mask of your oval shape, then apply it to the image.你应该参考这个.. 。
you should refer This ...
SWIFT 3 答案来自@Mohammad Sadiq
SWIFT 3 answer comes from @Mohammad Sadiq
这应该有效,
尝试将以下代码粘贴到 viewDidLoad() 中。
This should work,
Try pasting below code in viewDidLoad().
我移植了 Chris Nelson 的优秀Swift 5.10 的答案:
请注意,我没有以任何方式修改逻辑,只是让编译器满意的最低限度。
I have ported Chris Nelson's excellent answer to Swift 5.10:
Note that I haven't modified the logic in any way, just the bare minimum to keep the compiler happy.