Loading/Downloading image from URL on Swift

extension UIImageView {
    func downloaded(from url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {  // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {
                self.image = image
            }
        }.resume()
    }
    func downloaded(from link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {  // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
        guard let url = URL(string: link) else { return }
        downloaded(from: url, contentMode: mode)
    }
}


// Usage:
imageView.downloaded(from: "https://cdn.arstechnica.net/wp-content/uploads/2018/06/macOS-Mojave-Dynamic-Wallpaper-transition.jpg")

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
ios download image from url and save in device swift swift 5 download image from url load image with url swift image show from url without download in swift Swift 5 load image from url loading image from url swiftui swift uiimage download url file swift 5 get url of local file swiftui download image from url swift image load from url convert jpeg image url to png url in swift 5 convertjpeg image url to png in swift 5 ios download image from url swift swift download image from url to file image url scaltofit swift how to download image from data ios swift load the images from url in swift 5 image view load from url swift 5 without downloading image view load from url swift 5 nsurl load image in swift 5 get image url to data swift 4 swift load url image load image url swift swift 5 create a url for image with text in string and dynamic name swift 5 display image from url in a list swift 5 display image from url image view with url in swift 5 download image for galler in swift ios swift set image from url Data from Image url in swift async download picture from url swift set image url how to put a string url in a uiimageview ios IOS swift how to set image url in a imageview how to load image in swift 4 siwift get image url display image url swift load the imare url in swift 5 multiple image url is no loading in the imageviw in swift 5 image url is no loading in the imageviw in swift 5 setting an image of image viuwe as url swift let imageUrl = await uploadResult.text(); let url = data[i].url; photo url swift to image how to download image from url in swift swift assets image from api swift uiiamge from url dowload image from url swift ios display image from url programmatically objective c uiimageview load xcode uiimage from url swift 5 swift 5 uiimage from url ios UIImage load url ios get network image get image url from api swift ios 13 display image from url to uiimageview get url from image in swift load imgage from url swift how to download image by url in swift what is display image url and full size image url in xcode swift ui image from url download image in swift xcode get image https swift get data image from url add image with url swift how to add uiimage in string in swift to send in url get image from url urlsession swift 5 get image from url swift urlsession get image from url in swift getting png image from api in swift getting png image from api in swift\ getting png image from api in swit how to show png data in swift getting png from api in swift getting png from api in swit without uiimageView download image download images in swift downloadimages in swift load url image swift swift fetch image from server how to assign url to image view swift UIMAge url swift get image from url url image in swift url to image in swift image from url in swift load image url in swift swift 5 load image from url async ios download image from url display in uiimage ios app load https image swift fastest way to download url images String(contentsOf background string contents of url in background thread swift swift load image uri image url to data uri swift swift download image from url image downloader in swift download image from changing url swift get image from url swift setIamge and get Image swift class swift get image data from url download image with url swift swift how to download many images quickly how to add image from url swift staoyboarsd how to get image from url in swift image(completion: @escaping (UIImage?, String?) -> ()) -> String? download image from web url swift get image form urlresponse swift\ download image from url swift convert remote image to api swift swift load image url aztec swift sd image load img download image to device swift download image swift get png data from url swift load image from url swift load image from url to uiimageview swift get uiimage from url swift ios uiimageview load image from url objective c load image from url in image view ios swift url to image swift 5 swift 5 uiimageview load drom data swift 5 set image with data image url use like uiimage swift didplay a image from url in swift swift load image from url ios load image to image view swift download array of images async swift ios download image from url programmatically objective-c swift image api download show image from url swift how to set image from url swift show in swift image from web swift change image view with image url image form url to uiimage swift Swift downloading a photo from a url working with image URLs in Swift how to download image from url swift download images from url swift display image from url swift uiimage swift src url ui image swift src url download image swift uiimage swift uiimage from url swift 4 uiimage from url json image url programmaticallyk swift swift how to get an image from url how to user a image url in swift image swift image from url uiimage from url swift Loading/Downloading image from URL on Swift
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source