Swiftpack.co - malt03/Macduff as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by malt03.
malt03/Macduff 0.0.2
Macduff is a library for downloading, caching and displaying images on SwiftUI.
⭐️ 4
🕓 3 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/malt03/Macduff.git", from: "0.0.2")

Macduff Build Status Coverage Status Carthage compatible SwiftPM compatible License

I used Kingfisher as reference. Great thanks to onevcat !

Macduff is a library for downloading, caching and displaying images on SwiftUI.
You can create ProgressView or ErrorView, and customize ImageView.

Usage

struct ContentView: View {
    @State var imageURL = URL(string: "http://example.com/image")
    var body: some View {
      RemoteImage(with: imageURL)
    }
}

This is all you need to do!
Of course, if the imageURL is updated, the displayed image will also be updated.

Requirements

  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
  • Xcode 11.0+

Installation

SwiftPM (Recommended)

  • On Xcode, click File > Swift Packages > Add Package Dependency...
  • Input https://github.com/malt03/Macduff.git

Carthage

  • Insert github "malt03/Macduff" to your Cartfile.
  • Run carthage update.
  • Link your app with Macduff.framework in Carthage/Build.

CocoaPods

Since cocoapods does not yet support Xcode11, Macduff has not yet been released for cocoapods.

  • Insert pod 'Macduff' to your Podfile.
  • Run pod install.

Advanced Example

  • Set cornerRadius and frame on RemoteImage.
  • Custom placeholders for downloading and errors.
  • Custom view for displaying image.
  • Blur downloaded image.
  • Print log when the task finished.
struct ContentView: View {
    @State var imageURL = URL(string: "http://example.com/image")
    var body: some View {
        RemoteImage(
            with: imageURL,
            imageView: { Image(uiImage: $0).resizable().rotationEffect(.degrees(180)) },
            loadingPlaceHolder: { ProgressView(progress: $0) },
            errorPlaceHolder: { ErrorView(error: $0) },
            config: Config(transition: .scale, imageProcessor: GaussianBlurImageProcessor()),
            completion: { (status) in
                switch status {
                case .success(let image): print("success! imageSize:", image.size)
                case .failure(let error): print("failure... error:", error.localizedDescription)
                }
            }
        ).frame(width: 100, height: 100, alignment: .center).cornerRadius(50)
    }
}
struct ProgressView: View {
    let progress: Float
    var body: some View {
        return GeometryReader { (geometry) in
            ZStack(alignment: .bottom) {
                Rectangle().fill(Color.gray)
                Rectangle().fill(Color.green)
                    .frame(width: nil, height: geometry.frame(in: .global).height * CGFloat(self.progress), alignment: .bottom)
            }
        }
    }
}
struct ErrorView: View {
    let error: Error
    var body: some View {
        ZStack {
            Rectangle().fill(Color.red)
            Text(error.localizedDescription).font(Font.system(size: 8))
        }
    }
}

More Functions

Downloading Image without RemoteView

let fetcher = ImageFetcher(with: URL(string: "http://example.com/image")!)

/* 
These variables are private because the build doesn't pass due to a bug in Xcode.
They will be made public as soon as the bug is resolved.
*/
// fetcher.$progress.sink { print($0) }.store(in: &cancellables)
// fetcher.$image.sink { print($0?.size) }.store(in: &cancellables)
// fetcher.$error.sink { print($0?.localizedDescription) }.store(in: &cancellables)

fetcher.fetch { (status) in
    switch status {
    case .success(let image): print(image.size)
    case .failure(let error): print(error.localizedDescription)
    }
}

Removing disk cache

try DiskCache()?.removeAll()

Only memory cache

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Config.default = Config(cache: MemoryCache(fallbackCache: nil))
        return true
    }
}

Custom Image Processor

private struct MyImageProcessor1: ImageProcessor {
    var cacheKey: String { "com.malt03.MyImageProcessor1" }
    func process(image: NativeImage) -> NativeImage? {
        // process image...
        return image
    }
}

private struct MyImageProcessor2: ImageProcessor {
    var cacheKey: String { "com.malt03.MyImageProcessor2" }
    func process(image: NativeImage) -> NativeImage? {
        // process image...
        return image
    }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Config.default = Config(imageProcessor: [MyImageProcessor1(), MyImageProcessor2()])
        return true
    }
}

GitHub

link
Stars: 4
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

0.0.2
2 years ago

Support SwiftPM!

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