[Swift5] extension that allows UIImage to be specified by a String type URL

What is specified by a String type URL?

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.

Code introduction

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

[Swift5] extension that allows UIImage to be specified by a String type URL
[Swift5] How to create a .gitignore file and the code that should be written by default
[Java] Divide a character string by a specified character
[Swift] Converts a UInt64 type integer to [UInt8]
[java tool] A tool that deletes files under the specified path by extension
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
Convert to a tag to URL string in Rails
Convert from C String pointer to Swift String type
Convert a string to a character-by-character array with swift
[Java] How to cut out a character string character by character
URL to String conversion
[Java] How to convert a character string from String type to byte type
Find a Switch statement that can be converted to a Switch expression
How to select a specified date by code in FSCalendar
I tried to convert a string to a LocalDate type in Java
Convert an array that may be null to a stream
How to make a key pair of ecdsa in a format that can be read by Java