|
1 | 1 | # AsyncDownSamplingImage
|
2 | 2 |
|
3 |
| -A description of this package. |
| 3 | +`AsyncDownSamplingImage` is a SwiftUI component that has similar interface to original [AsyncImage](https://developer.apple.com/documentation/swiftui/asyncimage) and can perform downsampling so that we can reduce the memory buffer to store image data fetched from a server. |
| 4 | + |
| 5 | +# Impact of Downsampling |
| 6 | + |
| 7 | +with downsampling, we can reduce the huge amount of memory use like the below. |
| 8 | + |
| 9 | +|default AsyncImage| AsyncDownSamplingImage (×2~3 efficient) | |
| 10 | +|---|---| |
| 11 | +|<img width="480" alt="Screenshot 2023-02-03 at 21 58 31" src="https://user-images.githubusercontent.com/44002126/216665559-7f4efbc5-c649-4f25-b9fa-95f5ca60cf67.png">|<img width="480" alt="Screenshot 2023-02-03 at 21 58 48" src="https://user-images.githubusercontent.com/44002126/216665576-f1b994a7-b7ac-49d3-8e44-bda8f1b64130.png">| |
| 12 | + |
| 13 | + |
| 14 | +Moreover, the more the number of images increases, the more we can get the benefit. |
| 15 | + |
| 16 | +Below is a comparision when I scrolled and show 100 high resolution images (1000×1000px). |
| 17 | + |
| 18 | +|default AsyncImage| AsyncDownSamplingImage (×10~ efficient) | |
| 19 | +|---|---| |
| 20 | +|<img width="480" alt="Screenshot 2023-02-04 at 2 11 31" src="https://user-images.githubusercontent.com/44002126/216666328-6d4ea99c-45d4-48d0-960d-b162a9155413.png">|<img width="480" alt="Screenshot 2023-02-04 at 2 12 06" src="https://user-images.githubusercontent.com/44002126/216666337-0e079274-5a55-4469-b9ae-4c4dfc5b838d.png">| |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +# How to use AsyncDownSamplingImage |
| 25 | + |
| 26 | +`AsyncDownSamplingImage` aims to be used in a similar way to `AsyncImage` even if the implementation is different. |
| 27 | + |
| 28 | +```swift |
| 29 | +public init( |
| 30 | + url: Binding<URL?>, |
| 31 | + downsampleSize: Binding<CGSize>, |
| 32 | + content: @escaping (Image) -> Content, |
| 33 | + placeholder: (() -> Placeholder)?, |
| 34 | + fail: @escaping (Error) -> Fail |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +```swift |
| 39 | +public init( |
| 40 | + url: Binding<URL?>, |
| 41 | + downsampleSize: Binding<CGSize>, |
| 42 | + content: @escaping (Image) -> Content, |
| 43 | + fail: @escaping (Error) -> Fail |
| 44 | +) |
| 45 | +``` |
| 46 | + |
| 47 | +```swift |
| 48 | +public init( |
| 49 | + url: Binding<URL?>, |
| 50 | + downsampleSize: CGSize, |
| 51 | + content: @escaping (Image) -> Content, |
| 52 | + fail: @escaping (Error) -> Fail |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +You can use `AsyncDownSamplingImage` in the following way. |
| 57 | + |
| 58 | +```swift |
| 59 | + |
| 60 | +@State private var url = URL(string: "https://via.placeholder.com/1000") |
| 61 | +@State private var size: CGSize = .init(width: 160, height: 160) |
| 62 | + |
| 63 | +... |
| 64 | + |
| 65 | +AsyncDownSamplingImage( |
| 66 | + url: $url, |
| 67 | + downsampleSize: size |
| 68 | +) { image in |
| 69 | + image |
| 70 | + .resizable() |
| 71 | + .frame(width: size.width, height: size.height) |
| 72 | +} fail: { error in |
| 73 | + Text("Error: \(error.localizedDescription)") |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +# Contributing |
| 80 | + |
| 81 | +Pull requests, bug reports and feature requests are welcome 🚀 |
| 82 | + |
| 83 | +# License |
| 84 | + |
| 85 | +[MIT LICENSE](https://github.com/fummicc1/AsyncDownSamplingImage/blob/main/LICENSE) |
0 commit comments