如何在swift中为UIImage设置边框半径?

时间:2017-07-23 21:00:51

标签: swift uiimageview

我想要一个边界半径为21%的UIImage,使它看起来像一个app图标。我使用以下代码:

cell.imageView?.image = image
cell.imageView?.layer.cornerRadius = 21
//cell.imageView?.layer.borderWidth = 2.0
cell.imageView?.layer.masksToBounds = true

这个问题是它使图像变圆,这不是我想要的。我只是希望它看起来像方形,好像图标是用于应用图片,基本上是等效于border-radius: 21%;的HTML

我将如何实现这一目标?

1 个答案:

答案 0 :(得分:4)

cornerRadius设置为21会将其设置为21磅,而不是图像大小的21%。

您需要计算图像尺寸的21%。

cell.imageView?.layer.cornerRadius = image.size.width * 0.21

这假设你想要21%的图像宽度。