Skip to content

Instantly share code, notes, and snippets.

@zaimramlan
Created May 20, 2019 00:12
Show Gist options
  • Save zaimramlan/24161bebcef0ecf87fbe4c8e05ff68f6 to your computer and use it in GitHub Desktop.
Save zaimramlan/24161bebcef0ecf87fbe4c8e05ff68f6 to your computer and use it in GitHub Desktop.
[Swift] Scaled Asset URL String
import UIKit
extension String {
private var isURLString: Bool {
return URL(string: self) != nil
}
private var isAssetURL: Bool {
let asset = self.components(separatedBy: ".").last
return asset == "png" || asset == "jpg" || asset == "pdf"
}
var scaledAssetURL: String {
let scale = Int(UIScreen.main.scale)
guard isURLString, isAssetURL, scale != 1 else { return self }
var path = self
let components = path.components(separatedBy: ".")
let asset = components.last ?? "png"
path = components.dropLast().joined(separator: ".")
return "\(path)@\(scale)x.\(asset)"
}
}
let path = "https://www.domain.com/app-assets/asset.png"
path.scaledAssetURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment