Skip to content

Instantly share code, notes, and snippets.

@kylehowells
Created July 3, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylehowells/c92bbb7471f8ca919084817113815285 to your computer and use it in GitHub Desktop.
Save kylehowells/c92bbb7471f8ca919084817113815285 to your computer and use it in GitHub Desktop.
private func AVMakeRectFill(aspectRatio: CGSize, insideRect boundingRect: CGRect) -> CGRect {
let targetSize = boundingRect.size
if targetSize == .zero {
return .zero
}
let widthRatio = targetSize.width / aspectRatio.width
let heightRatio = targetSize.height / aspectRatio.height
let scalingFactor = max(widthRatio, heightRatio)
let newSize = CGSize(
width: aspectRatio.width * scalingFactor,
height: aspectRatio.height * scalingFactor
)
let origin = CGPoint(
x: (targetSize.width - newSize.width) / 2,
y: (targetSize.height - newSize.height) / 2
)
return CGRect(origin: origin, size: newSize)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment