Skip to content

Instantly share code, notes, and snippets.

@aaronlab
Created August 16, 2021 11:09
Show Gist options
  • Save aaronlab/6d1c2cadb80bf056044ef2083dc00bc1 to your computer and use it in GitHub Desktop.
Save aaronlab/6d1c2cadb80bf056044ef2083dc00bc1 to your computer and use it in GitHub Desktop.
Resolution
import UIKit
struct Resolution {
let width: CGFloat
let height: CGFloat
}
let resolution1 = Resolution(width: 1_920, height: 1_080)
let resolution2 = Resolution(width: 1_080, height: 1_920)
let maxWidthOrHeight: CGFloat = 1_280
func resize(resolution: Resolution) -> Resolution {
let width = resolution.width
let height = resolution.height
var ratio: CGFloat {
if width > height {
return maxWidthOrHeight / width
} else {
return maxWidthOrHeight / height
}
}
let newWidth: CGFloat = resolution.width * ratio
let newHeight: CGFloat = resolution.height * ratio
let newResolution = Resolution(width: newWidth, height: newHeight)
return newResolution
}
print(resize(resolution: resolution1))
print(resize(resolution: resolution2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment