Created
August 16, 2021 11:09
-
-
Save aaronlab/6d1c2cadb80bf056044ef2083dc00bc1 to your computer and use it in GitHub Desktop.
Resolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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