Skip to content

Commit 5606897

Browse files
authored
Merge pull request #9 from SDWebImage/example_upgrade
Update the example to support macOS. Use List to display both WebImage and AnimatedImage
2 parents 269a150 + 1b9a091 commit 5606897

File tree

14 files changed

+161
-31
lines changed

14 files changed

+161
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ Pods/
3737

3838
# SwiftPM
3939
.build
40+
.swiftpm

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.

Example/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ use_frameworks!
33
target 'SDWebImageSwiftUIDemo' do
44
platform :ios, '13.0'
55
pod 'SDWebImageSwiftUI', :path => '../'
6+
pod 'SDWebImageWebPCoder'
67
end

Example/Podfile.lock

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
PODS:
2-
- SDWebImage (5.1.0):
3-
- SDWebImage/Core (= 5.1.0)
4-
- SDWebImage/Core (5.1.0)
5-
- SDWebImageSwiftUI (0.1.1):
2+
- libwebp (1.0.3):
3+
- libwebp/demux (= 1.0.3)
4+
- libwebp/mux (= 1.0.3)
5+
- libwebp/webp (= 1.0.3)
6+
- libwebp/demux (1.0.3):
7+
- libwebp/webp
8+
- libwebp/mux (1.0.3):
9+
- libwebp/demux
10+
- libwebp/webp (1.0.3)
11+
- SDWebImage (5.2.2):
12+
- SDWebImage/Core (= 5.2.2)
13+
- SDWebImage/Core (5.2.2)
14+
- SDWebImageSwiftUI (0.1.2):
615
- SDWebImage (~> 5.1)
16+
- SDWebImageWebPCoder (0.2.5):
17+
- libwebp (~> 1.0)
18+
- SDWebImage/Core (~> 5.0)
719

820
DEPENDENCIES:
921
- SDWebImageSwiftUI (from `../`)
22+
- SDWebImageWebPCoder
1023

1124
SPEC REPOS:
1225
https://github.com/cocoapods/specs.git:
26+
- libwebp
1327
- SDWebImage
28+
- SDWebImageWebPCoder
1429

1530
EXTERNAL SOURCES:
1631
SDWebImageSwiftUI:
1732
:path: "../"
1833

1934
SPEC CHECKSUMS:
20-
SDWebImage: fb387001955223213dde14bc08c8b73f371f8d8f
21-
SDWebImageSwiftUI: fa0b13b16a92985532cd13931b88aea4ff7efb0b
35+
libwebp: 057912d6d0abfb6357d8bb05c0ea470301f5d61e
36+
SDWebImage: 5fcdb02cc35e05fc35791ec514b191d27189f872
37+
SDWebImageSwiftUI: 1026ac69b616d77b3ecb5b34e5b977ca9a165d5f
38+
SDWebImageWebPCoder: 947093edd1349d820c40afbd9f42acb6cdecd987
2239

23-
PODFILE CHECKSUM: 146734166216dd8fc1597433cc675999454ed4b2
40+
PODFILE CHECKSUM: c85d310339d014183658205b527a83237d24f8f2
2441

2542
COCOAPODS: 1.7.5

Example/SDWebImageSwiftUI.xcodeproj/project.pbxproj

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
320CDC3222FADB45007CF858 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 320CDC3122FADB45007CF858 /* Assets.xcassets */; };
1414
320CDC3522FADB45007CF858 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 320CDC3422FADB45007CF858 /* Preview Assets.xcassets */; };
1515
320CDC3822FADB45007CF858 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 320CDC3622FADB45007CF858 /* LaunchScreen.storyboard */; };
16+
326B0D712345C01900D28269 /* DetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326B0D702345C01900D28269 /* DetailView.swift */; };
1617
CECA1658ECBAF54E3FF3EF58 /* Pods_SDWebImageSwiftUIDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A371F81C3B5BD6972F7A0E2 /* Pods_SDWebImageSwiftUIDemo.framework */; };
1718
/* End PBXBuildFile section */
1819

@@ -27,6 +28,7 @@
2728
320CDC3422FADB45007CF858 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
2829
320CDC3722FADB45007CF858 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2930
320CDC3922FADB45007CF858 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
31+
326B0D702345C01900D28269 /* DetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailView.swift; sourceTree = "<group>"; };
3032
3E9F8B5F06960FFFBD1A5F99 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
3133
4A371F81C3B5BD6972F7A0E2 /* Pods_SDWebImageSwiftUIDemo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImageSwiftUIDemo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3234
54859B427E0A79E823713963 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
@@ -67,6 +69,7 @@
6769
320CDC2B22FADB44007CF858 /* AppDelegate.swift */,
6870
320CDC2D22FADB44007CF858 /* SceneDelegate.swift */,
6971
320CDC2F22FADB44007CF858 /* ContentView.swift */,
72+
326B0D702345C01900D28269 /* DetailView.swift */,
7073
320CDC3122FADB45007CF858 /* Assets.xcassets */,
7174
320CDC3622FADB45007CF858 /* LaunchScreen.storyboard */,
7275
320CDC3922FADB45007CF858 /* Info.plist */,
@@ -173,7 +176,6 @@
173176
TargetAttributes = {
174177
320CDC2822FADB44007CF858 = {
175178
CreatedOnToolsVersion = 11.0;
176-
ProvisioningStyle = Automatic;
177179
};
178180
};
179181
};
@@ -219,11 +221,15 @@
219221
"${PODS_ROOT}/Target Support Files/Pods-SDWebImageSwiftUIDemo/Pods-SDWebImageSwiftUIDemo-frameworks.sh",
220222
"${BUILT_PRODUCTS_DIR}/SDWebImage/SDWebImage.framework",
221223
"${BUILT_PRODUCTS_DIR}/SDWebImageSwiftUI/SDWebImageSwiftUI.framework",
224+
"${BUILT_PRODUCTS_DIR}/SDWebImageWebPCoder/SDWebImageWebPCoder.framework",
225+
"${BUILT_PRODUCTS_DIR}/libwebp/libwebp.framework",
222226
);
223227
name = "[CP] Embed Pods Frameworks";
224228
outputPaths = (
225229
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
226230
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImageSwiftUI.framework",
231+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImageWebPCoder.framework",
232+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/libwebp.framework",
227233
);
228234
runOnlyForDeploymentPostprocessing = 0;
229235
shellPath = /bin/sh;
@@ -260,6 +266,7 @@
260266
buildActionMask = 2147483647;
261267
files = (
262268
320CDC2C22FADB44007CF858 /* AppDelegate.swift in Sources */,
269+
326B0D712345C01900D28269 /* DetailView.swift in Sources */,
263270
320CDC2E22FADB44007CF858 /* SceneDelegate.swift in Sources */,
264271
320CDC3022FADB44007CF858 /* ContentView.swift in Sources */,
265272
);
@@ -292,9 +299,10 @@
292299
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
293300
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
294301
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
295-
CODE_SIGN_STYLE = Automatic;
302+
CODE_SIGN_IDENTITY = "";
296303
DEBUG_INFORMATION_FORMAT = dwarf;
297304
DEVELOPMENT_ASSET_PATHS = "SDWebImageSwiftUIDemo/Preview\\ Content";
305+
DEVELOPMENT_TEAM = "";
298306
ENABLE_PREVIEWS = YES;
299307
GCC_C_LANGUAGE_STANDARD = gnu11;
300308
INFOPLIST_FILE = SDWebImageSwiftUIDemo/Info.plist;
@@ -304,6 +312,9 @@
304312
MTL_FAST_MATH = YES;
305313
PRODUCT_BUNDLE_IDENTIFIER = com.dreampiggy.SDWebImageSwiftUIDemo;
306314
PRODUCT_NAME = "$(TARGET_NAME)";
315+
PROVISIONING_PROFILE_SPECIFIER = "";
316+
"PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = "";
317+
SUPPORTS_MACCATALYST = YES;
307318
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
308319
SWIFT_VERSION = 5.0;
309320
TARGETED_DEVICE_FAMILY = "1,2";
@@ -323,8 +334,9 @@
323334
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
324335
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
325336
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
326-
CODE_SIGN_STYLE = Automatic;
337+
CODE_SIGN_IDENTITY = "";
327338
DEVELOPMENT_ASSET_PATHS = "SDWebImageSwiftUIDemo/Preview\\ Content";
339+
DEVELOPMENT_TEAM = "";
328340
ENABLE_PREVIEWS = YES;
329341
GCC_C_LANGUAGE_STANDARD = gnu11;
330342
INFOPLIST_FILE = SDWebImageSwiftUIDemo/Info.plist;
@@ -333,6 +345,9 @@
333345
MTL_FAST_MATH = YES;
334346
PRODUCT_BUNDLE_IDENTIFIER = com.dreampiggy.SDWebImageSwiftUIDemo;
335347
PRODUCT_NAME = "$(TARGET_NAME)";
348+
PROVISIONING_PROFILE_SPECIFIER = "";
349+
"PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = "";
350+
SUPPORTS_MACCATALYST = YES;
336351
SWIFT_VERSION = 5.0;
337352
TARGETED_DEVICE_FAMILY = "1,2";
338353
};

Example/SDWebImageSwiftUIDemo/AppDelegate.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88

99
import UIKit
10+
import SDWebImage
11+
import SDWebImageWebPCoder
1012

1113
@UIApplicationMain
1214
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -15,6 +17,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1517

1618
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1719
// Override point for customization after application launch.
20+
// Add WebP support
21+
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
1822
return true
1923
}
2024

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,74 @@
77
*/
88

99
import SwiftUI
10+
import SDWebImage
1011
import SDWebImageSwiftUI
1112

13+
extension String : Identifiable {
14+
public typealias ID = Int
15+
public var id: Int {
16+
self.hashValue
17+
}
18+
}
19+
20+
let imageURLs = [
21+
"http://assets.sbnation.com/assets/2512203/dogflops.gif",
22+
"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
23+
"http://apng.onevcat.com/assets/elephant.png",
24+
"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
25+
"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
26+
"http://littlesvr.ca/apng/images/SteamEngine.webp",
27+
"http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
28+
"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
29+
"https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic",
30+
"https://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
31+
"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
32+
"http://via.placeholder.com/200x200.jpg"]
33+
1234
struct ContentView: View {
13-
var url: URL?
35+
@State var animated: Bool = false
1436

1537
var body: some View {
16-
VStack {
17-
WebImage(url: URL(string: "https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic"))
18-
.resizable()
19-
.scaledToFit()
20-
.frame(width: CGFloat(300), height: CGFloat(300), alignment: .center)
21-
AnimatedImage(url: URL(string: "https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif"), options: [.progressiveLoad])
22-
.resizable()
23-
.scaledToFill()
24-
.frame(width: CGFloat(400), height: CGFloat(300), alignment: .center)
38+
NavigationView {
39+
List(imageURLs) { url in
40+
NavigationLink(destination: DetailView(url: url, animated: self.animated)) {
41+
HStack {
42+
if self.animated {
43+
AnimatedImage(url: URL(string:url))
44+
.resizable()
45+
.scaledToFit()
46+
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
47+
} else {
48+
WebImage(url: URL(string:url))
49+
.resizable()
50+
.scaledToFit()
51+
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
52+
}
53+
Text(url)
54+
}
55+
}
56+
}
57+
.navigationBarTitle(animated ? "AnimatedImage" : "WebImage")
58+
.navigationBarItems(leading:
59+
Button(action: { self.reloadCache() }) {
60+
Text("Reload")
61+
}, trailing:
62+
Button(action: { self.switchView() }) {
63+
Text("Switch")
64+
}
65+
)
2566
}
2667
}
68+
69+
func reloadCache() {
70+
SDImageCache.shared.clearMemory()
71+
SDImageCache.shared.clearDisk(onCompletion: nil)
72+
}
73+
74+
func switchView() {
75+
SDImageCache.shared.clearMemory()
76+
animated.toggle()
77+
}
2778
}
2879

2980
#if DEBUG
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) DreamPiggy <lizhuoli1126@126.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
import SwiftUI
10+
import SDWebImageSwiftUI
11+
12+
struct DetailView: View {
13+
let url: String
14+
let animated: Bool
15+
16+
var body: some View {
17+
Group {
18+
if animated {
19+
AnimatedImage(url: URL(string:url), options: [.progressiveLoad])
20+
.resizable()
21+
.scaledToFit()
22+
} else {
23+
WebImage(url: URL(string:url), options: [.progressiveLoad])
24+
.resizable()
25+
.scaledToFit()
26+
}
27+
}
28+
}
29+
}
30+
31+
#if DEBUG
32+
struct DetailView_Previews: PreviewProvider {
33+
static var previews: some View {
34+
DetailView(url: "https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic", animated: false)
35+
}
36+
}
37+
#endif

Example/SDWebImageSwiftUIDemo/Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<string>1</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
23+
<key>NSAppTransportSecurity</key>
24+
<dict>
25+
<key>NSAllowsArbitraryLoads</key>
26+
<true/>
27+
</dict>
2328
<key>UIApplicationSceneManifest</key>
2429
<dict>
2530
<key>UIApplicationSupportsMultipleScenes</key>

Example/Screenshot/1.jpg

-628 KB
Binary file not shown.

Example/Screenshot/Demo-iOS.png

910 KB
Loading

Example/Screenshot/Demo-macOS.png

416 KB
Loading

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,17 @@ cd Example
9898
pod install
9999
```
100100

101-
Then open the Xcode Workspace to run the demo application.
101+
Then open the Xcode Workspace to run the demo application. You can run it on iOS and macOS.
102102

103103
## Screenshot
104104

105-
![](Example/Screenshot/1.jpg)
105+
+ iOS Demo
106+
107+
<img src='https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-iOS.png' width=960 />
108+
109+
+ macOS Demo
110+
111+
<img src='https://raw.githubusercontent.com/SDWebImage/SDWebImageSwiftUI/master/Example/Screenshot/Demo-macOS.png' width=960 />
106112

107113
## Author
108114

0 commit comments

Comments
 (0)