Since ios 16 my function for redrawing images is not working properly

Viewed 34

This is my function as extension UIImage:

func scaledWithMaxWidthOrHeightValue(value: CGFloat, spacing: CGFloat = 0, backgroundColor: UIColor = .clear, line: Bool = false, printing: Bool = false) -> UIImage? {
    
    let width = self.size.width
    let height = self.size.height
    
    if width < value && height < value {
        return self
    }
    
    let ratio = width/height
    
    var newWidth = value
    var newHeight = value
    
    if ratio > 1 {
        newWidth = min(width, value)
        newHeight = height * (newWidth/width)
    } else {
        newHeight = min(height, value)
        newWidth = width * (newHeight/height)
    }
    
    let rangeWidth = newWidth + spacing * 2
    let rangeHeight = newHeight + spacing * 2
    UIGraphicsBeginImageContextWithOptions(CGSize(width: rangeWidth, height: rangeHeight), false, 0)
    let ctx = UIGraphicsGetCurrentContext()
    backgroundColor.setFill()
    ctx?.fill(CGRect(x: 0, y: 0, width: rangeWidth, height: rangeHeight))
    draw(in: CGRect(x: spacing, y: spacing, width: newWidth, height: newHeight))
    
    if printing {
        let length: CGFloat = 7
        let stroke: CGFloat = 0.5
        ctx?.setFillColor(UIColor.red.cgColor)
        ctx?.addRect(CGRect(x: spacing, y: 0, width: stroke, height: length))
        ctx?.addRect(CGRect(x: 0, y: spacing, width: length, height: stroke))
        ctx?.addRect(CGRect(x: rangeWidth - spacing, y: 0, width: stroke, height: length))
        ctx?.addRect(CGRect(x: rangeWidth - 8, y: spacing, width: length, height: stroke))
        ctx?.addRect(CGRect(x: rangeWidth - length, y: rangeHeight - spacing, width: length, height: stroke))
        ctx?.addRect(CGRect(x: rangeWidth - spacing, y: rangeHeight - length, width: stroke, height: length))
        ctx?.addRect(CGRect(x: spacing, y: rangeHeight - length, width: stroke, height: length))
        ctx?.addRect(CGRect(x: 0, y: rangeHeight - spacing, width: length, height: stroke))
        ctx?.drawPath(using: .fill)
    }
    
    if line {
        ctx?.setStrokeColor(UIColor.red.cgColor)
        ctx?.setLineWidth(0.5)
        ctx?.addEllipse(in: CGRect(x: spacing, y: spacing, width: newWidth, height: newHeight))
        ctx?.drawPath(using: .stroke)
    }
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return image
}

And in ios 16 it looks like this:

enter image description here

and before the update it was:

enter image description here

Please, tell me where is the crucial place that is causing the problem?

UPDATE: Downvoting doesnt change anything, because it is question like any other.

0 Answers
Related