GithubHelp home page GithubHelp logo

imageloader's Introduction

ImageLoader

ImageLoader with ProgressBar

Requirements

  • Xcode 13
  • iOS 15

How to load Image data

  • Use async/awiat method
func data(from url: URL) async throws -> Image {
    let (asyncBytes, urlResponse) = try await URLSession.shared.bytes(for: URLRequest(url: url))

    let length = Int(urlResponse.expectedContentLength)
    var data = Data()
    data.reserveCapacity(length)

    for try await byte in asyncBytes {
        data.append(byte)
        let progress = CGFloat(data.count) / CGFloat(length)
        ratio = progress
    }

    return Image(uiImage: UIImage(data: data)!)
}

Example

    @State var ratio: CGFloat = 0.0
    @State var image: Image?
    
    var body: some View {
        VStack {
            Button(action: {
                Task {
                    image = try! await data(from: URL(string: "https://source.unsplash.com/random/1000x1000")!)
                }
            }) {
                Text("Load Image")
            }
            ProgressBar(ratio: ratio)
                .frame(height: 20)
                .padding()
            
            HStack {
                if ratio < 1.0 {
                    ProgressView()
                    
                } else {
                    image?
                        .resizable()
                        .scaledToFit()
                }
            }
            .frame(width: 300, height: 300)
        }
    }

ImageLoader

imageloader's People

Contributors

phillip5094 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.