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

load image from url in Image swiftui (iOS 15)

AsyncImage(url: URL(string: "https://your_image_url_address"))
Comment

load image from url in Image swiftui (iOS 15)

AsyncImage(
  url: "https://dogecoin.com/assets/img/doge.png",
  transaction: .init(animation: .easeInOut)
) { image in
  image
    .resizable()
    .aspectRatio(contentMode: .fit)
}. placeholder: {
  Color.gray
}
  .frame(width: 500, height: 500)
  .mask(RoundedRectangle(cornerRadius: 16)
Comment

PREVIOUS NEXT
Code Example
Swift :: swift5 get uiview height 
Swift :: concatenate string swift 
Swift :: swift get slected row data in tableview cell 
Swift :: uitextview set placeholder text swift 5 
Swift :: swift file size from url 
Swift :: rounded ios button 
Swift :: swiftui font add 
Swift :: find range of string swift 
Swift :: how to dismiss a view when touch up inside swift 
Swift :: ios UIButton change image 
Swift :: swift check if array values are equal 
Swift :: xcode execute code after when navigating back to screen 
Swift :: can you pass an enum as a parameter to a function swift 
Swift :: swift enum xib 
Swift :: swiftui image aspect ratio 
Swift :: Swift Expressions 
Swift :: Swift Add Two Numbers 
Swift :: Swift guard Vs if Statement 
Swift :: get absolution position of view in swift 
Swift :: how to darken view swiftui 
Swift :: list header swiftui 
Swift :: Swift Operator Associativity 
Swift :: do something when your HTTP response finishes. swift 
Swift :: swift_SmtpTransport with oauth2 
Swift :: ternary operator in swift 
Swift :: print 1 line swift 
Swift :: swift variable 
Ruby :: ruby file extension 
Ruby :: devise add trackable 
Ruby :: rails link_to class 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =