Swiftpack.co -  kean/FetchImage as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
kean/FetchImage
Makes it easy to download images using Nuke and display them in SwiftUI apps
.package(url: "https://github.com/kean/FetchImage.git", from: "0.4.0")

FetchImage makes it easy to download images using Nuke and display them in SwiftUI apps.

Overview

FetchImage is an observable object (ObservableObject) that allows you to manage the download of an image and observe the results of the download.

public final class FetchImage: ObservableObject, Identifiable {
    /// Returns the fetched image.
    ///
    /// - note: In case pipeline has `isProgressiveDecodingEnabled` option enabled
    /// and the image being downloaded supports progressive decoding, the `image`
    /// might be updated multiple times during the download.
    @Published public private(set) var image: PlatformImage?

    /// Returns an error if the previous attempt to fetch the most recent attempt
    /// to load the image failed with an error.
    @Published public private(set) var error: Error?

    /// Returns `true` if the image is being loaded.
    @Published public private(set) var isLoading: Bool = false

    /// The progress of the image download.
    @Published public var progress = Progress()
}

Usage

FetchImage doesn't ship an image view because it's trivial to create one using SwiftUI and customize it precisely the way you want.

struct ImageView: View {
    let url: URL

    @StateObject private var image = FetchImage()

    var body: some View {
        ZStack {
            Rectangle().fill(Color.gray)
            image.view?
                .resizable()
                .aspectRatio(contentMode: .fill)
                .clipped()
        }
        .onAppear { image.load(url) }
        .onChange(of: url) { image.load($0) }
        .onDisappear(perform: image.reset)
    }
}

For iOS 13, use @ObservedObject. Keep in mind that @ObservedObject does not own the instance; you need to maintain a strong reference to the FetchImage instance. Instead of onChange(of:), add .id(url) to your ImageView – it will ensure that onAppear is called when the URL changes.

Usage with a list:

struct DetailsView: View {
    var body: some View {
        List(imageUrls, id: \.self) {
            ImageView(url: $0)
                .frame(height: 200)
        }
    }
}

FetchImage gives you full control over how to manage the download and display the image. For example, if you want the download to continue when the view leaves the screen, change the appearance callbacks:

.onAppear {
    image.priority = .normal
    image.load(url)
}
.onDisappear {
    image.priority = .low
}

Animations:

struct ImageView: View {
    let url: URL

    @StateObject private var image = FetchImage()

    var body: some View {
        // ... create image view 
        .onAppear {
            // Ensure that memory cache lookup is performed without animations
            withoutAnimation {
                image.load(url)
            }
        }
        .onDisappear(perform: image.reset)
        .animation(.default)
    }
}

private func withoutAnimation(_ closure: () -> Void) {
    var transaction = Transaction(animation: nil)
    transaction.disablesAnimations = true
    withTransaction(transaction, closure)
}

Requirements

Nuke Swift Xcode Platforms
FetchImage Swift 5.1 Xcode 11.3 iOS 13.0 / watchOS 6.0 / macOS 10.15 / tvOS 13.0

License

FetchImage is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 202
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

FetchImage 0.4.0
9 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API