I currently have some code to resize big image down to screen size and keep aspect ratio to save memory usage as the loaded image could be huge. My current code is like this
import UIKit
import func AVFoundation.AVMakeRect
let uiImage = <UIImage load from source>
let screenRect = UIScreen.main.bounds
let rect = AVMakeRect(aspectRatio: image.size, insideRect: screenRect)
let renderer = UIGraphicsImageRenderer(size: screenRect.size)
try? renderer.image { _ in
image.draw(in: rect)
}.heicData()?.write(to: <some image store path>)
I will then load image from store path later when necessary.
This works but if the image aspect ratio is different from the screen aspect ratio, this method will add some additional transparent surroundings. How could I avoid those transparent surroundings? Thanks.