I check if the image is vertical or horizontal. If it is horizontal, I rotate it:
@IBOutlet private weak var img: UIImageView!
img.image = file.image
let imageSize = file.image?.size
let imgWidth = imageSize?.width ?? 0
let imgHeight = imageSize?.height ?? 0
if imgWidth > imgHeight {
print("IMG HORIZONTAL")
imgDetail.transform = imgDetail.transform.rotated(by: .pi / 2)
} else {
print("IMG VERTICAL")
}
But it leaves me a space around the image. I would like it to be at the maximum size of the UIImageView.


Try this, declare your imageView:
in viewDidLoad set if statement and call setupConstraints:
set up constraints
write image extension
this is the result normal (without image rotate extension call) and rotated (with image rotate extension call):