When you specify an image with UIImage, you may want to get the URL from the web and define the image with that URL as UIImage. (URL cannot be specified by default)
Extensions are useful in such cases. This time, I will introduce such an extension.
extension UIImage {
public convenience init(url: String) {
let url = URL(string: url)
do {
let data = try Data(contentsOf: url!)
self.init(data: data)!
return
} catch let err {
print("Error : \(err.localizedDescription)")
}
self.init()
}
}
If you define this, you can specify the URL in UIImage. ** By the way, it completes the code! ** **
Recommended Posts