Skip to content

Instantly share code, notes, and snippets.

@kylehowells
Created July 3, 2022 20:02
Show Gist options
  • Save kylehowells/c92bbb7471f8ca919084817113815285 to your computer and use it in GitHub Desktop.
Save kylehowells/c92bbb7471f8ca919084817113815285 to your computer and use it in GitHub Desktop.

Revisions

  1. kylehowells created this gist Jul 3, 2022.
    21 changes: 21 additions & 0 deletions AVMakeRectFill.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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)
    }