我有 UIImageView 具有不同类型的形状(圆形或矩形或椭圆形或六边形)图像。当用户选择时,我需要更改背景并添加红色边框。 尝试使用 layer.borderColor 和 layer.borderWidth,但它总是在图像视图周围绘制矩形。还试过功能:
func drawOutline(imageKeof: CGFloat = 0.2, color: UIColor = .white) -> UIImage? {
let outlinedImageRect = CGRect(x: 0.0, y: 0.0, width: size.width * imageKeof, height: size.height * imageKeof)
let imageRect = CGRect(x: size.width * (imageKeof - 1) * 0.5, y: size.height * (imageKeof - 1) * 0.5, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size, false, imageKeof)
draw(in: outlinedImageRect)
let context = UIGraphicsGetCurrentContext()
context!.setBlendMode(.sourceIn)
context!.setFillColor(color.cgColor)
context!.fill(outlinedImageRect)
draw(in: imageRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
它在原始图像周围绘制边框,但我只需要白色背景而不是图像。