Swiftpack.co - Package - SDWebImage/SDWebImageSwiftUI

SDWebImageSwiftUI

CI Status Version License Platform Carthage compatible SwiftPM compatible

What's for

This is an experimental project for SDWebImage.

It aims to ensure the following function available for users and try to do some experiments for Swift platform.

  • Swift Package Manager integration
  • SwiftUI compatibility
  • Swift source code compatibility

Note we do not guarantee the public API stable for current status until v1.0 version. Since SwiftUI is a new platform for us, we need to investigate the API design.

Requirements

  • Xcode 11+
  • iOS 13+
  • macOS 10.15+
  • tvOS 13+
  • watchOS 6+
  • Swift 5.1+

Installation

CocoaPods

SDWebImageSwiftUI is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SDWebImageSwiftUI'

Carthage

SDWebImageSwiftUI is available through Carthage.

github "SDWebImage/SDWebImageSwiftUI"

Swift Package Manager

SDWebImageSwiftUI is available through Swift Package Manager.

let package = Package(
    dependencies: [
        .package(url: "https://github.com/SDWebImage/SDWebImageSwiftUI.git", from: "0.3")
    ],
)

Usage

Using WebImage to load network image

  • [x] Supports the placeholder and detail options control for image loading as SDWebImage.
  • [x] Supports the success/failure/progress changes event for custom handling.

Note: Unlike UIImageView in UIKit, SwiftUI's Image does not support animation. This WebImage using Image for internal implementation and supports static image format only.

var body: some View {
    WebImage(url: URL(string: "https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic"))
        .onSuccess(perform: { (image, cacheType) in
            // Success
        })
        .resizable()
        .scaledToFit()
        .frame(width: 300, height: 300, alignment: .center)
}

Using AnimatedImage to play animation

var body: some View {
    Group {
        // Network
        AnimatedImage(url: URL(string: "https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif"))
        .onFailure(perform: { (error) in
            // Error
        })
        .scaledToFit()
        // Data
        AnimatedImage(data: try! Data(contentsOf: URL(fileURLWithPath: "/tmp/foo.webp")))
        .customLoopCount(1)
        // Bundle (not Asset Catalog)
        AnimatedImage(name: "animation1", isAnimating: $isAnimating)) // Animation control binding
        .maxBufferSize(.max)
    }
}
  • [x] Supports network image as well as local data and bundle image
  • [x] Supports animation control using the SwiftUI Binding
  • [x] Supports advanced control like loop count, incremental load, buffer size.

Note: AnimatedImage supports both image url or image data for animated image format. Which use the SDWebImage's Animated ImageView for internal implementation.

Note: AnimatedImage does not support watchOS currently. Because there are no Public API to implement it via WKInterfaceObjectRepresentable or SwiftUI.Image. We need Apple to provide a better solution. But if you're really interested in this, checkout feature_watchOS_animation branch, which using some super hack to support animated image on watchOS.

Demo

To run the example using SwiftUI, following the steps:

cd Example
pod install

Then open the Xcode Workspace to run the demo application.

Since SwiftUI is aimed to support all Apple platforms, our demo does this as well, one codebase including:

  • iOS (iPhone/iPad/Mac Catalyst)
  • macOS
  • tvOS
  • watchOS

Tips:

  1. Use Switch (right-click on macOS) to switch between WebImage and AnimatedImage.
  2. Use Reload (right-click on macOS/force press on watchOS) to clear cache.
  3. Use Swipe to delete one image item.

Screenshot

  • iOS Demo
  • macOS Demo
  • tvOS Demo
  • watchOS Demo

Author

DreamPiggy

License

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

Github

link
Stars: 60
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

0.3.3 - Oct 20, 2019

Fixes

  • Try to stop animating when dismantle UIViewRepresentable, fix the potential leak (bug ?) for AnmatedImage #21

0.3.2 - Oct 10, 2019

Fixes

  • Fix WebImage showing empty image when entering edit mode #20

0.3.1 - Oct 5, 2019

Fixes

  • Using the frame layout for view wrapper instead of Autolayout. Solve the issue when running on AppKit (not Catalyst) #17

Example

  • Update the demo to support all Apple platforms, using one codebase #18

0.3.0 - Oct 5, 2019

Features

  • Supports AnimatedImage advanced control for custom loop count / max buffer size / incremental load #13
  • Supports AnimatedImage animation control using of SwiftUI Binding #14

Documentation

  • Complete all current API documentation #15

Enhancement

  • Use the @Published instead of hard-written protocol implementation #12

0.2.0 - Oct 3, 2019

Features

  • Supports onSuccess/onFailure/onProgress callback for WebImage/AnimatedImage #10

Fixes

  • Fix the placeholder support for AnimatedImage #11

Demo

  • Update the example to support macOS. Use List to display both WebImage and AnimatedImage #9