     func buildGradient(in size: CGSize, start: UIColor, end: UIColor) -> UIImage? {
 
         UIGraphicsBeginImageContext(size)
+        defer { UIGraphicsEndImageContext() }
+
         let context = UIGraphicsGetCurrentContext()!
 
         let colorSpace = CGColorSpaceCreateDeviceRGB()

        super.layoutSubviews()

        guard let textSize = (text as NSString?)?.size(withAttributes: [NSAttributedStringKey.font: font]) else { return }
        if let bgImage = buildGradient(in: textSize, start: startColor, end: endColor) {
            let gradientColor = UIColor(patternImage: bgImage)
            textColor = gradientColor
        }
    }

        let endPoint = CGPoint(x: size.width / 2, y: size.height)
        context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: [])

        return UIGraphicsGetImageFromCurrentImageContext()
    }
}
