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-08-07 18:51:28 +08:00
This is an experimental project for [SDWebImage ](https://github.com/SDWebImage/SDWebImage ).
2019-08-09 18:29:29 +08:00
It aims to ensure the following function available for users and try to do some experiments for Swift platform.
2019-08-07 18:51:28 +08:00
+ Swift Package Manager integration
+ SwiftUI compatibility
+ Swift source code compatibility
2019-08-07 18:10:05 +08:00
2019-09-25 03:11:33 +08:00
Note we do not guarantee the public API stable for current status. Since SwiftUI is a new platform for us, we need to investigate the API design.
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-03 18:17:44 +08:00
.package(url: "https://github.com/SDWebImage/SDWebImageSwiftUI.git", from: "0.2")
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-05 15:02:44 +08:00
- [x] Supports the placeholder and detail options control for image loading as SDWebImage.
- [x] Supports the success/failure/progress changes event for custom handling.
2019-08-07 18:51:28 +08:00
2019-10-01 16:32:35 +08:00
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.
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-03 18:13:19 +08:00
.onSuccess(perform: { (image, cacheType) in
// Success
})
2019-10-01 16:32:35 +08:00
.resizable()
2019-08-07 18:51:28 +08:00
.scaledToFit()
.frame(width: 300, height: 300, alignment: .center)
}
```
2019-10-05 15:02:44 +08:00
### Using `AnimatedImage` to play animation
2019-08-07 18:51:28 +08:00
```swift
var body: some View {
2019-10-05 15:02:44 +08:00
Group {
AnimatedImage(url: URL(string: "https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif"))
.onFailure(perform: { (error) in
// Error
})
.scaledToFit()
AnimatedImage(data: try! Data(contentsOf: URL(fileURLWithPath: "/tmp/foo.webp")))
.customLoopCount(1)
}
2019-08-07 18:51:28 +08:00
}
```
2019-10-05 15:02:44 +08:00
- [x] Supports network image as well as local data and bundle image
- [x] Supports advanced control like loop count, incremental load, buffer size.
2019-10-01 16:32:35 +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.
2019-08-07 18:51:28 +08:00
## Demo
To run the example using SwiftUI, following the steps:
```
cd Example
pod install
```
2019-10-03 14:42:29 +08:00
Then open the Xcode Workspace to run the demo application. You can run it on iOS and macOS.
2019-08-07 18:51:28 +08:00
## Screenshot
2019-10-03 14:42:29 +08:00
+ iOS Demo
< img src = 'https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-iOS.png' width = 960 / >
+ macOS Demo
< img src = 'https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-macOS.png' width = 960 / >
2019-08-07 18:51:28 +08:00
2019-08-07 18:10:05 +08:00
## Author
2019-08-07 18:51:28 +08:00
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