CAGradientLayer different between iOS 12 and iOS 13

Viewed 85

I use this code to generate an UIImage from a gradient.

func buildGradientImage() -> UIImage? {
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
    gradientLayer.type = .radial
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradientLayer.endPoint = CGPoint(x: 1.2, y: 1.0)
    gradientLayer.colors = [UIColor.red.cgColor, UIColor.green.cgColor]
    
    var gradientImage:UIImage?
    UIGraphicsBeginImageContext(gradientLayer.frame.size)
    if let context = UIGraphicsGetCurrentContext() {
        gradientLayer.render(in: context)
        gradientImage = UIGraphicsGetImageFromCurrentImageContext()
    }
    UIGraphicsEndImageContext()
    return gradientImage
}

For some reason the resulting image is different between iOS 12 and iOS 13-14. Red seems to expand more on the iOS 12 gradient.

Someone know why there is this difference? Shouldn't the code generate the same gradient?

Gradient difference between iOS 13-14 and iOS 12

0 Answers
Related