Saturday, November 7, 2026
HomeiOS Developmentios - UIGraphicsImageRenderer utilized in a loop ends with reminiscence difficulty

ios – UIGraphicsImageRenderer utilized in a loop ends with reminiscence difficulty

[ad_1]

I’ve a easy operate like this:

func card(from pictures: [UIImage]) -> CardImage { //synchronic manner
    let renderer = UIGraphicsImageRenderer(dimension: CGSize(width: radius, peak: radius))
    var coordinates = [ImageCoordinate]()
    
    let picture = renderer.picture { [weak self] context in
        let dimension = renderer.format.bounds.dimension
        self?.backgroundColor.setFill()
        context.cgContext.fillEllipse(in: CGRect(x: 0, y: 0, width: dimension.width, peak: dimension.peak))

        let set = units.random
        for (index, picture) in pictures.enumerated() {
            if let positions = set?.positions[index], let cgimage = picture.flippedVertically?.cgImage {
                context.cgContext.translateBy(x: CGFloat(positions.x), y: CGFloat(positions.y))
                context.cgContext.rotate(by: CGFloat(positions.r))
                context.cgContext.translateBy(x: CGFloat(-positions.w/2), y: CGFloat(-positions.h/2))
                let width = CGFloat(positions.w)
                let peak = CGFloat(positions.h)
                var newWidth: CGFloat = 0
                var newHeight: CGFloat = 0
                let imageWidth = picture.dimension.width
                let imageHeight = picture.dimension.peak
                let ratio = imageWidth / imageHeight
                if ratio > 1 {
                    newWidth = CGFloat(width)
                    newHeight = newWidth / imageWidth * imageHeight
                } else {
                    newHeight = CGFloat(peak)
                    newWidth = newHeight / imageHeight * imageWidth
                }
                context.cgContext.translateBy(x: CGFloat((width - newWidth) / 2), y: CGFloat((peak - newHeight) / 2))
                context.cgContext.draw(cgimage, in: CGRect(x: 0, y: 0, width: newWidth, peak: newHeight))
                context.cgContext.translateBy(x: CGFloat(-(width - newWidth) / 2), y: CGFloat(-(peak - newHeight) / 2))
                context.cgContext.translateBy(x: CGFloat(positions.w/2), y: CGFloat(positions.h/2))
                context.cgContext.rotate(by: CGFloat(-positions.r))
                context.cgContext.translateBy(x: CGFloat(-positions.x), y: CGFloat(-positions.y))
                coordinates.append(ImageCoordinate(x: positions.x, y: positions.y, w: Double(newWidth), h: Double(newHeight), r: positions.r))
            }
        }
    }
    return CardImage(picture: picture, coordinates: coordinates)
}

I feel it isn’t sophisticated as a lot as a result of I do right here quite a lot of employees with delivered set of pictures (all the time 8 pictures), however once I do it with a loop for i in 1...57 it finally ends up with reminiscence difficulty (app is closed after ~31 iterations). Why?

How can I keep away from that? Is there a technique to repair that downside?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments