Draw border around content of UIImageView

前端 未结 2 513
我在风中等你
我在风中等你 2020-12-29 11:48

I\'ve a UIImageView with a image with is a car with a transparent background:

\"enter<

2条回答
  •  旧巷少年郎
    2020-12-29 12:28

    haawa answer for Swift 5

    extension UIImage {
        func drawOutlie(imageKeof: CGFloat = 1.01, color: UIColor) -> 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)
    
            guard let context = UIGraphicsGetCurrentContext() else {return nil}
            context.setBlendMode(.sourceIn)
            context.setFillColor(color.cgColor)
            context.fill(outlinedImageRect)
            draw(in: imageRect)
    
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return newImage
        }
    }
    

提交回复
热议问题