Skip to content

Instantly share code, notes, and snippets.

@pouriaalmassi
Created November 9, 2019 00:28
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 pouriaalmassi/34d74101e4f31e812988c16790449fe0 to your computer and use it in GitHub Desktop.
Save pouriaalmassi/34d74101e4f31e812988c16790449fe0 to your computer and use it in GitHub Desktop.
Random Swift image placeholder URL.
extension URL {
/// Use `let url = URL.placeholderImageUrl`
static var placeholderImageUrl: URL? {
let size = Int.random(in: 150..<800)
return placeholderImageUrl(with: CGSize(width: size, height: size))
}
/// Use `let url = URL.placeholderImageUrl(with: CGSize(width: 200, height: 200))`
static func placeholderImageUrl(with size: CGSize) -> URL? {
let services = [
"www.fillmurray.com",
"www.placecage.com",
"www.stevensegallery.com",
"placekitten.com",
"placebear.com",
]
return URL(string: "https://\(services[Int.random(in: 0..<services.count)])/\(Int(size.width))/\(Int(size.height))")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment