article

Wednesday, June 9, 2021

SwiftUI fetch image from URL

SwiftUI fetch image from URL
 
//
//  ContentView.swift
//  Test
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        Image(systemName: "person.fill")
            .data(url: URL(string: "https://freepngimg.com/thumb/one_piece/23231-5-one-piece-chibi-image-thumb.png")!)
            .scaleEffect()
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension Image {
    func data(url:URL) -> Self {
        if let data = try? Data(contentsOf: url) {
            return Image(uiImage: UIImage(data: data)!)
                .resizable()
        }
        return self
            .resizable()
    }
}

Related Post