結局↓こうなりました。 extension UIImageView { public var viewToImage: CGAffineTransform? { guard let image = self.image else { return nil } switch contentMode { case .scaleAspectFit: let viewSize = frame.size let imageSize = image.size if viewSize.width / viewSize.height < imageSize.width / imageSize.height { return UIImageView.viewToImageFittingHorizontally(viewSize: viewSize, imageSize: imageSize) } else { return UIImageView.viewToImageFittingVertically(viewSize: viewSize, imageSize: imageSize) } case .scaleAspectFill: let viewSize = frame.size let imageSize = image.size if viewSize.width / viewSize.height < imageSize.width / imageSize.height { return UIImageView.viewToImageFittingVertically(viewSize: viewSize, imageSize: imageSize) } else { return UIImageView.viewToImageFittingHorizontally(viewSize: viewSize, imageSize: imageSize) } default: fatalError("Unsupported `contentMode`: \(contentMode)") } } private static func viewToImageFittingHorizontally(viewSize: CGSize, imageSize: CGSize) -> CGAffineTransform { let scale = imageSize.width / viewSize.width let offsetY = (imageSize.height - viewSize.height * scale) / 2 return CGAffineTransform(a: scale, b: 0, c: 0, d: scale, tx: 0, ty: offsetY) } private static func viewToImageFittingVertically(viewSize: CGSize, imageSize: CGSize) -> CGAffineTransform { let scale = imageSize.height / viewSize.height let offsetX = (imageSize.width - viewSize.width * scale) / 2 return CGAffineTransform(a: scale, b: 0, c: 0, d: scale, tx: offsetX, ty: 0) } }