Good morning, when using the following extension I'm getting a memory leak when using Instruments, I've tried capturing self with a weak reference but I still get the leak.
public extension UIImage {
func resize(_ targetSize: CGSize) -> UIImage {
let size = self.size
// Calculate the scaling ratios
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
// Determine the scale factor and size to fill the target size
let scaleFactor = max(widthRatio, heightRatio)
let scaledImageSize = CGSize(
width: size.width * scaleFactor,
height: size.height * scaleFactor
)
// Calculate the clipping rect
let clippingRect = CGRect(
x: (scaledImageSize.width - targetSize.width) / 2.0,
y: (scaledImageSize.height - targetSize.height) / 2.0,
width: targetSize.width,
height: targetSize.height
)
let format = UIGraphicsImageRendererFormat()
format.scale = 1
// Render the clipped image
let renderer = UIGraphicsImageRenderer(size: targetSize, format: format)
return autoreleasepool {
return renderer.image { _ in
self.draw(in: CGRect(
x: -clippingRect.origin.x,
y: -clippingRect.origin.y,
width: scaledImageSize.width,
height: scaledImageSize.height
))
}
}
}
}