検証環境:
Xcode 11.4
Swift 5.2
UIImageView の画像に AspectFit を適用した後の画像サイズは、AVFoundation の AVMakeRect(aspectRatio:insideRect:)
を使うと自分で計算せずに簡単にサイズを知ることができます。
UIImageView の Extension として実装してみました。
import AVFoundation extension UIImageView { var imageSize: CGSize { if let image = image { return AVMakeRect(aspectRatio: image.size, insideRect: bounds).size } return .zero } }
https://developer.apple.com/documentation/avfoundation/1390116-avmakerect