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

swift image

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

Image("dog")
Comment

PREVIOUS NEXT
Code Example
Swift :: how to change the color of back button navbar xcodee 
Swift :: swift add width/height constraint to view 
Swift :: string value of enum swift 
Swift :: swift completion handler 
Swift :: how to set the center in view in swift 
Swift :: swift create uinavigationcontroller programmatically 
Swift :: how to remove item from list swift 
Swift :: uiview set inside padding 
Swift :: swft image 
Swift :: navigationbarhidden not working swiftui 
Swift :: rounded ios button 
Swift :: create button with icon swift 
Swift :: swift array map to another array 
Swift :: swift set keyboard next functionality 
Swift :: swift extension Array where element 
Swift :: convert secondsfrom1970 to date swift 
Swift :: swift switch 
Swift :: variable sum in swift 
Swift :: Swift Create String Instance 
Swift :: swift how to wait in a loop 
Swift :: UIApplicationWillEnterForeground 
Swift :: swift variables 
Swift :: swift UI color rgb 
Swift :: array of button listeners swift 
Swift :: Swift Find Number of Dictionary Elements 
Swift :: Swift for Loop With Range 
Swift :: swift 5 on return button action 
Swift :: remove child from scene swift 
Swift :: circular array swift 
Ruby :: rails remove column from model 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =