how to add colored border to uiimage in swift

后端 未结 5 1649
礼貌的吻别
礼貌的吻别 2020-12-31 15:59

It is pretty easy to add border to UIImageView, using layers (borderWidth, borderColor etc.). Is there any possibility to add border to image, not to image view? Does somebo

5条回答
  •  臣服心动
    2020-12-31 16:27

    Use this simple extension for UIImage

    extension UIImage {
    
        func outline() -> UIImage? {
    
            UIGraphicsBeginImageContext(size)
            let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            self.draw(in: rect, blendMode: .normal, alpha: 1.0)
            let context = UIGraphicsGetCurrentContext()
            context?.setStrokeColor(red: 1.0, green: 0.5, blue: 1.0, alpha: 1.0)
            context?.setLineWidth(5.0)
            context?.stroke(rect)
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return newImage
    
        }
    
    }
    

    It will give you an image with pink border.

提交回复
热议问题