Skip to content

Commit accd13a

Browse files
authored
Merge pull request #3 from fummicc1/dev
Update README and Example
2 parents f7c5a04 + f1f94c7 commit accd13a

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

Example/Example/ContentView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct ContentView: View {
2727
VStack {
2828
ScrollView {
2929
LazyVGrid(columns: [.init(), .init()]) {
30-
ForEach(0..<1000, id: \.self) { _ in
30+
ForEach(0..<100, id: \.self) { _ in
3131
AsyncDownSamplingImage(
3232
url: $url,
3333
downsampleSize: size
@@ -51,7 +51,7 @@ struct ContentView: View {
5151
VStack {
5252
ScrollView {
5353
LazyVGrid(columns: [.init(), .init()]) {
54-
ForEach(0..<1000, id: \.self) { _ in
54+
ForEach(0..<100, id: \.self) { _ in
5555
AsyncImage(url: url) { phase in
5656
switch phase {
5757
case .empty:

README.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,85 @@
11
# AsyncDownSamplingImage
22

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

Comments
 (0)