Created
May 20, 2019 00:12
-
-
Save zaimramlan/24161bebcef0ecf87fbe4c8e05ff68f6 to your computer and use it in GitHub Desktop.
[Swift] Scaled Asset URL String
This file contains 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 | |
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