Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

display image from url swift

let imageCache = NSCache<AnyObject, AnyObject>()

extension UIImageView {
    func setImage(from url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        contentMode = mode
        
        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() {
                imageCache.setObject(imageToCache, forKey: url as AnyObject)
                self.image = imageToCache
            }
        }.resume()
    }
    func setImage(from link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        setImage(from: url, contentMode: mode)
    }
}
Comment

swift image

//Make sure the image you want to show is imported into your "Assets.xcassets" file.

Image("dog")
Comment

how to add an image in swift

//The first step is to rename the image you want to add into your xcode project
//Then, drag the picture to the Assets.xcassests folder.
//Next, use the Image struct and init(name) to display the picture

//For example, if your picture in the Assets.xcassests folder is named daddy, then to get the picture, use

Image("daddy")

//There are more modifiers to the Image but that will not be discussed here
Comment

PREVIOUS NEXT
Code Example
Swift :: uilabel make bold 
Swift :: remove all add TapGestureRecognizer swift 
Swift :: swiftui coin flip 
Swift :: swift convert string to int 
Swift :: swift multiline string 
Swift :: swift iterate through string 
Swift :: swift create an empty array 
Swift :: how to get the path of selected PDF/doc from file manager in ios swift programmatically 
Swift :: swiftui textfield focus 
Swift :: uilabel set font 
Swift :: swift while loop 
Swift :: did select row at 
Swift :: sort list ios swift 
Swift :: swift lazy 
Swift :: two variable sum in swift 
Swift :: check if UIView is UIButton or UILabel not 
Swift :: Swift Floating-point Literals 
Swift :: Swift Weak Reference 
Swift :: swift increase int value 
Swift :: create a dictionary in swift 
Swift :: swift hmac256 
Swift :: how to call another view controller method when button click from another ios swift 
Swift :: Swift Nested Function with Return Values 
Swift :: swift dictionary to json string online 
Swift :: swift computed property 
Swift :: Swift print() with terminator 
Swift :: didselectrowatindexpath not called swift 
Ruby :: A Ruby write to file example 
Ruby :: rails get current database name 
Ruby :: ruby replace first character in string 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =