2019-08-07 18:10:05 +08:00
# SDWebImageSwiftUI
2019-08-09 17:27:11 +08:00
[![CI Status ](https://travis-ci.org/SDWebImage/SDWebImageSwiftUI.svg?branch=master )](https://travis-ci.com/SDWebImage/SDWebImageSwiftUI)
2019-08-07 18:10:05 +08:00
[![Version ](https://img.shields.io/cocoapods/v/SDWebImageSwiftUI.svg?style=flat )](https://cocoapods.org/pods/SDWebImageSwiftUI)
[![License ](https://img.shields.io/cocoapods/l/SDWebImageSwiftUI.svg?style=flat )](https://cocoapods.org/pods/SDWebImageSwiftUI)
[![Platform ](https://img.shields.io/cocoapods/p/SDWebImageSwiftUI.svg?style=flat )](https://cocoapods.org/pods/SDWebImageSwiftUI)
2019-08-09 16:28:54 +08:00
[![Carthage compatible ](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat )](https://github.com/Carthage/Carthage)
[![SwiftPM compatible ](https://img.shields.io/badge/SwiftPM-Compatible-brightgreen.svg )](https://swift.org/package-manager/)
2019-08-07 18:10:05 +08:00
2019-08-07 18:51:28 +08:00
## What's for
2019-08-07 18:10:05 +08:00
2019-10-24 01:07:11 +08:00
SDWebImageSwiftUI is a SwiftUI image loading framework, which based on [SDWebImage ](https://github.com/SDWebImage/SDWebImage ).
2019-08-07 18:51:28 +08:00
2019-10-24 01:07:11 +08:00
It brings all your favorite features from SDWebImage, like async image loading, memory/disk caching, animated image playback and performances.
2019-08-07 18:51:28 +08:00
2019-11-01 03:54:12 +08:00
## Features
2019-11-01 12:40:21 +08:00
Since SDWebImageSwiftUI is built on top of SDWebImage, it provide both the out-of-box features as well as advanced powerful features you may want in real world Apps. Check our [Wiki ](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage ) when you need:
2019-11-01 03:54:12 +08:00
- [x] Animated Image full-stack solution, with balance of CPU && RAM
- [x] Progressive image loading, with animation support
- [x] Reusable download, never request single URL twice
- [x] URL Request / Response Modifier, provide custom HTTP Header
- [x] Image Transformer, apply corner radius or CIFilter
- [x] Multiple caches system, query from different source
- [x] Multiple loaders system, load from different resource
You can also get all benefits from the existing community around with SDWebImage. You can have massive image format support (GIF/APNG/WebP/HEIF/AVIF) via [Coder Plugins ](https://github.com/SDWebImage/SDWebImage/wiki/Coder-Plugin-List ), PhotoKit support via [SDWebImagePhotosPlugin ](https://github.com/SDWebImage/SDWebImagePhotosPlugin ), Firebase integration via [FirebaseUI ](https://github.com/firebase/FirebaseUI-iOS ), etc.
2019-10-29 21:08:59 +08:00
2019-10-24 01:07:11 +08:00
Besides all these features, we do optimization for SwiftUI, like Binding, View Modifier, using the same design pattern to become a good SwiftUI citizen.
2019-08-07 18:10:05 +08:00
2019-11-01 03:58:28 +08:00
Note we do not guarantee the public API stable for current status until v1.0 version, to follow [Semantic Versioning ](https://semver.org/ ).
2019-11-02 05:41:49 +08:00
This framework is under heavily development, it's recommended to use [the latest release ](https://github.com/SDWebImage/SDWebImageSwiftUI/releases ) as much as possible (including SDWebImage dependency). All feature requests, contributions, and GitHub stars are welcomed.
2019-08-09 17:11:33 +08:00
2019-08-07 18:10:05 +08:00
## Requirements
2019-08-07 18:51:28 +08:00
+ Xcode 11+
+ iOS 13+
+ macOS 10.15+
+ tvOS 13+
+ watchOS 6+
2019-10-01 16:32:35 +08:00
+ Swift 5.1+
2019-08-07 18:51:28 +08:00
2019-08-07 18:10:05 +08:00
## Installation
2019-08-09 16:15:44 +08:00
#### CocoaPods
2019-08-07 18:10:05 +08:00
SDWebImageSwiftUI is available through [CocoaPods ](https://cocoapods.org ). To install
it, simply add the following line to your Podfile:
```ruby
pod 'SDWebImageSwiftUI'
```
2019-08-09 16:15:44 +08:00
#### Carthage
SDWebImageSwiftUI is available through [Carthage ](https://github.com/Carthage/Carthage ).
```
2019-08-09 17:11:33 +08:00
github "SDWebImage/SDWebImageSwiftUI"
2019-08-09 16:15:44 +08:00
```
#### Swift Package Manager
SDWebImageSwiftUI is available through [Swift Package Manager ](https://swift.org/package-manager/ ).
```swift
let package = Package(
dependencies: [
2019-10-27 20:20:53 +08:00
.package(url: "https://github.com/SDWebImage/SDWebImageSwiftUI.git", from: "0.6")
2019-08-09 16:15:44 +08:00
],
)
```
2019-08-07 18:51:28 +08:00
## Usage
2019-10-05 15:02:44 +08:00
### Using `WebImage` to load network image
2019-08-07 18:51:28 +08:00
2019-10-26 04:26:07 +08:00
- [x] Supports placeholder and detail options control for image loading as SDWebImage
2019-11-02 05:37:35 +08:00
- [x] Supports progressive image loading
2019-10-26 04:26:07 +08:00
- [x] Supports success/failure/progress changes event for custom handling
- [x] Supports indicator with activity/progress indicator and customization
2019-10-27 19:46:40 +08:00
- [x] Supports built-in animation and transition, powered by SwiftUI
2019-08-07 18:51:28 +08:00
```swift
var body: some View {
2019-09-25 03:11:33 +08:00
WebImage(url: URL(string: "https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic"))
2019-10-24 00:44:19 +08:00
.onSuccess { image, cacheType in
2019-10-03 18:13:19 +08:00
// Success
2019-10-24 00:44:19 +08:00
}
2019-10-28 18:48:04 +08:00
.resizable() // Resizable like SwiftUI.Image
2019-11-02 14:12:20 +08:00
.placeholder {
Image(systemName: "photo") // Placeholder
}
2019-10-27 16:36:22 +08:00
.indicator(.activity) // Activity Indicator
2019-11-02 14:12:20 +08:00
.animation(.easeInOut(duration: 0.5)) // Animation Duration
2019-10-27 19:46:40 +08:00
.transition(.fade) // Fade Transition
2019-08-07 18:51:28 +08:00
.scaledToFit()
.frame(width: 300, height: 300, alignment: .center)
}
```
2019-10-29 21:08:59 +08:00
Note: This `WebImage` using `Image` for internal implementation, which is the best compatible for SwiftUI layout and animation system. But it supports static image format only, because unlike `UIImageView` in UIKit, SwiftUI's `Image` does not support animation.
2019-10-05 15:02:44 +08:00
### Using `AnimatedImage` to play animation
2019-08-07 18:51:28 +08:00
2019-10-29 21:08:59 +08:00
- [x] Supports network image as well as local data and bundle image
2019-11-02 05:37:35 +08:00
- [x] Supports animated progressive image loading
2019-10-29 21:08:59 +08:00
- [x] Supports animation control using the SwiftUI Binding
- [x] Supports indicator and transition, powered by SDWebImage and Core Animation
- [x] Supports advanced control like loop count, incremental load, buffer size
- [x] Supports coordinate with native UIKit/AppKit/WatchKit view
2019-08-07 18:51:28 +08:00
```swift
var body: some View {
2019-10-05 15:02:44 +08:00
Group {
2019-10-05 15:52:56 +08:00
// Network
AnimatedImage(url: URL(string: "https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif"))
2019-10-24 00:44:19 +08:00
.onFailure { error in
2019-10-05 15:02:44 +08:00
// Error
2019-10-24 00:44:19 +08:00
}
2019-10-28 18:48:04 +08:00
.resizable() // Actually this is not needed unlike SwiftUI.Image
2019-11-02 14:12:20 +08:00
.placeholder(UIImage(systemName: "photo")) // Placeholder
2019-10-27 19:46:40 +08:00
.indicator(SDWebImageActivityIndicator.medium) // Activity Indicator
2019-10-26 03:47:28 +08:00
.transition(.fade) // Fade Transition
2019-10-29 21:24:20 +08:00
.scaledToFit() // Attention to call it on AnimatedImage, but not `some View` after View Modifier
2019-10-05 15:52:56 +08:00
// Data
AnimatedImage(data: try! Data(contentsOf: URL(fileURLWithPath: "/tmp/foo.webp")))
2019-10-05 15:02:44 +08:00
.customLoopCount(1)
2019-10-29 21:24:20 +08:00
2019-10-05 15:52:56 +08:00
// Bundle (not Asset Catalog)
AnimatedImage(name: "animation1", isAnimating: $isAnimating)) // Animation control binding
2019-10-05 15:12:25 +08:00
.maxBufferSize(.max)
2019-11-02 16:39:56 +08:00
.onViewUpdate { view, context in // Advanced native view coordinate
view.toolTip = "Mouseover Tip"
}
2019-10-05 15:02:44 +08:00
}
2019-08-07 18:51:28 +08:00
}
```
2019-10-27 19:46:40 +08:00
Note: `AnimatedImage` supports both image url or image data for animated image format. Which use the SDWebImage's [Animated ImageView ](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-image-50 ) for internal implementation. Pay attention that since this base on UIKit/AppKit representable, some advanced SwiftUI layout and animation system may not work as expected. You may need UIKit/AppKit and Core Animation to modify the native view.
2019-08-07 18:51:28 +08:00
2019-10-22 01:25:29 +08:00
Note: From v0.4.0, `AnimatedImage` supports watchOS as well. However, it's not backed by SDWebImage's [Animated ImageView ](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-image-50 ) like iOS/tvOS/macOS. It use some tricks and hacks because of the limitation on current Apple's API. It also use Image/IO decoding system, which means it supports GIF and APNG format only, but not external format like WebP.
2019-10-19 22:47:10 +08:00
2019-10-29 21:24:20 +08:00
### Which View to choose
2019-10-29 21:08:59 +08:00
2019-10-29 21:24:20 +08:00
Why we have two different View types here, is because of current SwiftUI limit. But we're aimed to provide best solution for all use cases.
2019-10-29 21:08:59 +08:00
2019-10-29 21:24:20 +08:00
If you don't need animated image, prefer to use `WebImage` firstly. Which behaves the seamless as built-in SwiftUI View. If SwiftUI works, it works.
2019-10-29 21:08:59 +08:00
2019-10-29 21:24:20 +08:00
If you need animated image, `AnimatedImage` is the one to choose. Remember it supports static image as well, you don't need to check the format, just use as it.
2019-10-29 21:08:59 +08:00
But, because `AnimatedImage` use `UIViewRepresentable` and driven by UIKit, currently there may be some small incompatible issues between UIKit and SwiftUI layout and animation system. We try our best to match SwiftUI behavior, and provide the same API as `WebImage` , which make it easy to switch between these two types.
2019-11-02 05:37:35 +08:00
For more information, it's really recommended to check our demo below, to learn detailed API usage.
2019-08-07 18:51:28 +08:00
## Demo
To run the example using SwiftUI, following the steps:
```
cd Example
pod install
```
2019-10-05 20:17:51 +08:00
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
2019-11-02 05:49:01 +08:00
Demo Tips:
2019-10-05 20:17:51 +08:00
2019-11-02 05:49:01 +08:00
1. Use `Switch` (right-click on macOS/force press on watchOS) to switch between `WebImage` and `AnimatedImage` .
2019-10-05 20:17:51 +08:00
2. Use `Reload` (right-click on macOS/force press on watchOS) to clear cache.
2019-10-10 18:07:32 +08:00
3. Use `Swipe` to delete one image item.
2019-11-02 05:49:01 +08:00
4. Clear cache and go to detail page to see progressive loading.
2019-08-07 18:51:28 +08:00
## Screenshot
2019-10-03 14:42:29 +08:00
+ iOS Demo
2019-10-05 20:22:59 +08:00
< img src = 'https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-iOS.jpg' height = 960 / >
2019-10-03 14:42:29 +08:00
+ macOS Demo
2019-10-05 20:17:51 +08:00
< img src = 'https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-macOS.jpg' width = 960 / >
+ tvOS Demo
< img src = 'https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-tvOS.jpg' width = 960 / >
+ watchOS Demo
2019-10-05 20:22:59 +08:00
< img src = 'https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-watchOS.jpg' width = 480 / >
2019-08-07 18:51:28 +08:00
2019-10-24 01:07:11 +08:00
## Extra Notes
Besides all above things, this project can also ensure the following function available on Swift platform for SDWebImage itself.
+ SwiftUI compatibility
+ Swift Package Manager integration
+ Swift source code compatibility and Swifty
Which means, this project is one core use case and downstream dependency, which driven SDWebImage itself future development.
2019-08-07 18:10:05 +08:00
## Author
2019-11-01 03:54:12 +08:00
[DreamPiggy ](https://github.com/dreampiggy )
2019-08-07 18:10:05 +08:00
## License
SDWebImageSwiftUI is available under the MIT license. See the LICENSE file for more info.
2019-08-07 18:51:28 +08:00