如何剪切图像的角
How to cut corner of an image
我用 swift 文件
创建了这个视图
import UIKit
@IBDesignable class DesignView: UIView {
@IBInspectable var CornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = CornerRadius
}
}
}
但是当我设置图像背景时,视图的图像溢出是这样的:
我尝试像这样将相同的 swift 文件应用到我的图像视图
import UIKit
@IBDesignable class DesignImageView: UIImageView {
@IBInspectable var CornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = CornerRadius
}
}
}
但是没有任何附加内容,所以我如何在不编辑图像的情况下切角?
尝试
view.clipsToBound = true
您的图片因其尺寸而超出范围
Setting this value to true causes subviews to be clipped to the bounds of the receiver. If set to false, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is false.
从您的 Class Scope
中添加此代码
extension UIView // Almost any Kind of View inherits UIView Ex: UIImageView
{
func setCorner(withRadius radius : CGFloat)
{
self.layer.cornerRadius = radius
self.clipsToBounds = true //your view won't cross the bound
}
}
并像这样使用。 .
DesignImageView.setCorner(withRadius: 5)
我用 swift 文件
创建了这个视图
import UIKit
@IBDesignable class DesignView: UIView {
@IBInspectable var CornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = CornerRadius
}
}
}
但是当我设置图像背景时,视图的图像溢出是这样的:
我尝试像这样将相同的 swift 文件应用到我的图像视图
import UIKit
@IBDesignable class DesignImageView: UIImageView {
@IBInspectable var CornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = CornerRadius
}
}
}
但是没有任何附加内容,所以我如何在不编辑图像的情况下切角?
尝试
view.clipsToBound = true
您的图片因其尺寸而超出范围
Setting this value to true causes subviews to be clipped to the bounds of the receiver. If set to false, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is false.
从您的 Class Scope
extension UIView // Almost any Kind of View inherits UIView Ex: UIImageView
{
func setCorner(withRadius radius : CGFloat)
{
self.layer.cornerRadius = radius
self.clipsToBounds = true //your view won't cross the bound
}
}
并像这样使用。 .
DesignImageView.setCorner(withRadius: 5)