AsyncMultiplexImage

0.4.0

Async image loading for SwiftUI with support for multiple resolutions and efficient handling using Swift concurrency features.
FluidGroup/swiftui-async-multiplex-image

What's New

0.4.0

2023-07-04T15:52:24Z

What's Changed

Full Changelog: 0.3.0...0.4.0

AsyncMultiplexImage for SwiftUI

This library provides an asynchronous image loading solution for SwiftUI applications. It supports loading multiple image resolutions and automatically handles displaying the most appropriate image based on the available space. The library uses Swift's concurrency model, including actors and tasks, to manage image downloading efficiently.

Features

  • Asynchronous image downloading
  • Supports multiple image resolutions
  • Efficient image loading using Swift's concurrency model
  • Logging utilities for debugging and error handling

Installation

Swift Package Manager

Add the following dependency to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/YourGitHubUsername/AsyncMultiplexImage.git", from: "1.0.0")
]

Usage

  1. Import the library:
import AsyncMultiplexImage
  1. Define a MultiplexImage with a unique identifier and a closure that returns a list of URLs for different image resolutions:
let multiplexImage = MultiplexImage(identifier: "imageID", urlsProvider: { _ in
    [URL(string: "https://example.com/image_small.png")!,
     URL(string: "https://example.com/image_medium.png")!,
     URL(string: "https://example.com/image_large.png")!]
})
  1. Create an AsyncMultiplexImage view using the MultiplexImage and a custom downloader conforming to AsyncMultiplexImageDownloader:
struct MyImageView: View {
    let multiplexImage: MultiplexImage
    let downloader: MyImageDownloader

    var body: some View {
        AsyncMultiplexImage(multiplexImage: multiplexImage, downloader: downloader) { phase in
            switch phase {
            case .empty:
                ProgressView()
            case .progress(let image):
                image.resizable()
            case .success(let image):
                image.resizable()
            case .failure(let error):
                Text("Error: \(error.localizedDescription)")
            }
        }
    }
}
  1. Implement a custom image downloader conforming to AsyncMultiplexImageDownloader:
class MyImageDownloader: AsyncMultiplexImageDownloader {
    func download(candidate: AsyncMultiplexImageCandidate) async throws -> Image {
        // Download the image and return a SwiftUI.Image instance
    }
}

License

This library is available under the MIT License. See the LICENSE file for more information.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Fri Apr 05 2024 06:13:08 GMT-0900 (Hawaii-Aleutian Daylight Time)