uiimage swift src url

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")

0
10
Evan Harper 115 points

                                    let imageCache = NSCache<AnyObject, AnyObject>()

class CustomImageView: UIImageView {
    
    var ImageUrlString: URL?
    
    func setImage(from url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        contentMode = mode
        ImageUrlString = url
        if let imageFromCache = imageCache.object(forKey: url as AnyObject) {
            self.image = imageFromCache as? UIImage
            return
        }
        
        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 imageToCache = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {
                if self.ImageUrlString == url {
                    self.image = imageToCache
                }
                imageCache.setObject(imageToCache, forKey: url as AnyObject)
            }
        }.resume()
    }
    func setImage(from link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        setImage(from: url, contentMode: mode)
    }
}

0
0
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
convert url to uiimage swift ios download image from url and save in device swift url to image ios swift load image from url swift ios swift uiimage get jpg data swift image set uiimage swift uiimageview set url swift view uiimage set image by url in swift swift adding images from url swiftui get image from url how to get uiimage from assets swift show image image from url swift sdwebimage set in UIimage swift save image to url using sdimage swift get image from image path swiftui get image from profileimagepath swiftui convert image url to uiimage swift URL to Image SwiftUI how to set api url image in imageview in swift 5 how to get uiimage from url in swift swift 5 load image from url sdwebimage how to set image with url property in swift swift ui Image with url display image in swift with url how to access image url and set UIImage in swift how to get image file from url in swift show image with url swift load image from url in swiftUI Swift UIImageView set image from assets in swiftui swift get uiimage from url swift 5 download image from url get image from link swift uiimage from assets swift swiftui url images image from url swift ui SwiftUI get photo from url swiftui image from link set image swift uiimage uiimage from local url swift 5 nsimage from url swift 5 swift load uiimage from filepath swift load uiimage from file with name swift load uiimage from file swift get an image with path swiftui load image from path load images from url swift how to create image with url in swift img url in swift swift ios uiimageview load image from url load image with url swift swift 5 load and image from url to uiimageview image show from url without download in swift image load from url in swift get image from url in ios swift get uiimage from live phasset ios swift load images from url swiftui url to image swift create image object from url swift create uiimage from url swift Swift 5 load image from url Get image from URL SwiftUI set UIImage from url swift swift uiimage from local url UIImageView download image from url swift swift set image from url how to show image from url in swift save uiimage file swift url image swiftui swift 5 image from url loading image from url swiftui loading image from url swift swift uiimage download url file swift load image from url in view swift read image from url convert uiimage to url swift 5 How to render url to an image swift how to get image path from photo library swift swift load UIImage from path swift get UIImage from local path uiimage example swift sdwebimage ios uiimageview load image from url swift 4 swift display image from url image from url swift image from url swiftui load pic url in ios swift swift load uiimage from assets swift get uiimage from image save image in url swift image url to image in swift display image from url in swift URL Image view in SwiftUI swiftui show image from url swiftui fetch image from url swiftui image with url swiftui image from file url opening UIImage in swift swift ui set image locally swift ui get image swift 5 get image from url swift 5 get url of local file get image from url swift ui load image from url swift ui swift uiimage set image swiftui open image url urlsession image swift swift add image programmatically image url to uiimage swift ui image source from url swift change ui image source swift swiftui load image from url swiftui download image from url swiftui image load from url swiftui image from url get image as url swift set image from url swift swiftui URL Image get uiimage from assets swift set image from url ios swift swift image load from url swiftui image 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 transfer network image to matrix ios get network image get image url from api swift ios 13 display image from url to uiimageview set image from string in xcode 9.3 display image from string in xcode 9.3 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 load url image takes tim 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 swift url for saved image 2019 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 how to download an image from a url in 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 xcode click image to url 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 How to auto pass multiple images string value using 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