Skip to content

Commit 248515a

Browse files
committed
Add the Xcode project to support Carthage. Fix the build issue on macOS/tvOS/watchOS
1 parent e7914c0 commit 248515a

File tree

11 files changed

+976
-27
lines changed

11 files changed

+976
-27
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ DerivedData
2323
.bundle
2424

2525
# Add this line if you want to avoid checking in source code from Carthage dependencies.
26-
# Carthage/Checkouts
27-
26+
Carthage/Checkouts
2827
Carthage/Build
2928

3029
# We recommend against adding the Pods directory to your .gitignore. However

Cartfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github "SDWebImage/SDWebImage" ~> 5.1

Cartfile.resolved

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github "SDWebImage/SDWebImage" "5.1.0"

SDWebImageSwiftUI.xcodeproj/project.pbxproj

Lines changed: 834 additions & 0 deletions
Large diffs are not rendered by default.

SDWebImageSwiftUI.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,83 @@
11
//
2-
// Image+WebCache.swift
3-
// SDWebImageSwiftUIDemo
2+
// AnimatedImage.swift
3+
// SDWebImageSwiftUI
44
//
5-
// Created by lizhuoli on 2019/7/26.
5+
// Created by lizhuoli on 2019/8/9.
66
// Copyright © 2019 lizhuoli. All rights reserved.
77
//
88

99
import SwiftUI
1010
import SDWebImage
1111

12-
public struct AnimatedImage: UIViewRepresentable {
12+
#if !os(watchOS)
13+
14+
public struct AnimatedImage: ViewRepresentable {
1315
var url: URL?
1416
var name: String?
1517
var bundle: Bundle?
1618
var data: Data?
1719
var scale: Length = 0
1820

19-
public init(url: URL, placeholder: Image? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
20-
self.url = url
21+
#if os(macOS)
22+
public typealias NSViewType = SDAnimatedImageView
23+
#else
24+
public typealias UIViewType = SDAnimatedImageView
25+
#endif
26+
27+
#if os(macOS)
28+
public func makeNSView(context: NSViewRepresentableContext<AnimatedImage>) -> SDAnimatedImageView {
29+
makeView(context: context)
2130
}
2231

23-
public init(name: String, bundle: Bundle? = nil) {
24-
self.name = name
25-
self.bundle = bundle
32+
public func updateNSView(_ nsView: SDAnimatedImageView, context: NSViewRepresentableContext<AnimatedImage>) {
33+
updateView(nsView, context: context)
34+
}
35+
#else
36+
public func makeUIView(context: UIViewRepresentableContext<AnimatedImage>) -> SDAnimatedImageView {
37+
makeView(context: context)
2638
}
2739

28-
public init(data: Data, scale: Length = 0) {
29-
self.data = data
30-
self.scale = scale
40+
public func updateUIView(_ uiView: SDAnimatedImageView, context: UIViewRepresentableContext<AnimatedImage>) {
41+
updateView(uiView, context: context)
3142
}
43+
#endif
3244

33-
public func makeUIView(context: UIViewRepresentableContext<AnimatedImage>) -> SDAnimatedImageView {
45+
func makeView(context: ViewRepresentableContext<AnimatedImage>) -> SDAnimatedImageView {
3446
SDAnimatedImageView()
3547
}
3648

37-
public func updateUIView(_ uiView: SDAnimatedImageView, context: UIViewRepresentableContext<AnimatedImage>) {
49+
func updateView(_ view: SDAnimatedImageView, context: ViewRepresentableContext<AnimatedImage>) {
3850
if let url = url {
39-
uiView.sd_setImage(with: url)
51+
view.sd_setImage(with: url)
4052
return
4153
}
4254
if let name = name {
43-
uiView.image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil)
55+
#if os(macOS)
56+
view.image = SDAnimatedImage(named: name, in: bundle)
57+
#else
58+
view.image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil)
59+
#endif
4460
return
4561
}
4662
if let data = data {
47-
uiView.image = SDAnimatedImage(data: data, scale: scale)
63+
view.image = SDAnimatedImage(data: data, scale: scale)
4864
return
4965
}
5066
}
67+
68+
public init(url: URL, placeholder: Image? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
69+
self.url = url
70+
}
71+
72+
public init(name: String, bundle: Bundle? = nil) {
73+
self.name = name
74+
self.bundle = bundle
75+
}
76+
77+
public init(data: Data, scale: Length = 0) {
78+
self.data = data
79+
self.scale = scale
80+
}
5181
}
82+
83+
#endif

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//
22
// ImageManager.swift
3-
// Pods-SDWebImageSwiftUIDemo
3+
// SDWebImageSwiftUI
44
//
5-
// Created by lizhuoli on 2019/8/7.
5+
// Created by lizhuoli on 2019/8/9.
6+
// Copyright © 2019 lizhuoli. All rights reserved.
67
//
78

89
import SwiftUI
@@ -16,7 +17,7 @@ class ImageManager : BindableObject {
1617
private var manager = SDWebImageManager.shared
1718
private weak var currentOperation: SDWebImageOperation? = nil
1819

19-
var image: UIImage? {
20+
var image: Image? {
2021
willSet {
2122
willChange.send(self)
2223
}
@@ -37,7 +38,13 @@ class ImageManager : BindableObject {
3738

3839
func load() {
3940
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, _, _) in
40-
self.image = image
41+
if let image = image {
42+
#if os(macOS)
43+
self.image = Image(nsImage: image)
44+
#else
45+
self.image = Image(uiImage: image)
46+
#endif
47+
}
4148
}
4249
}
4350

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// SDWebImageSwiftUI.swift
3+
// SDWebImageSwiftUI
4+
//
5+
// Created by lizhuoli on 2019/8/9.
6+
// Copyright © 2019 lizhuoli. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import SwiftUI
11+
12+
#if !os(watchOS)
13+
14+
#if os(macOS)
15+
typealias ViewRepresentable = NSViewRepresentable
16+
typealias ViewRepresentableContext = NSViewRepresentableContext
17+
#else
18+
typealias ViewRepresentable = UIViewRepresentable
19+
typealias ViewRepresentableContext = UIViewRepresentableContext
20+
#endif
21+
22+
#endif

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// WebImage.swift
3-
// SDWebImageSwiftUIDemo
3+
// SDWebImageSwiftUI
44
//
5-
// Created by lizhuoli on 2019/7/26.
5+
// Created by lizhuoli on 2019/8/9.
66
// Copyright © 2019 lizhuoli. All rights reserved.
77
//
88

@@ -27,7 +27,7 @@ public struct WebImage : View {
2727

2828
public var body: some View {
2929
if let image = imageManager.image {
30-
return Image(uiImage: image)
30+
return image
3131
.resizable()
3232
.onAppear {}
3333
.onDisappear {}
@@ -37,7 +37,12 @@ public struct WebImage : View {
3737
.onAppear { self.imageManager.load() }
3838
.onDisappear { self.imageManager.cancel() }
3939
} else {
40-
return Image(uiImage: UIImage())
40+
#if os(macOS)
41+
let emptyImage = Image(nsImage: NSImage())
42+
#else
43+
let emptyImage = Image(uiImage: UIImage())
44+
#endif
45+
return emptyImage
4146
.resizable()
4247
.onAppear { self.imageManager.load() }
4348
.onDisappear { self.imageManager.cancel() }

SDWebImageSwiftUI/Module/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>$(CURRENT_PROJECT_VERSION)</string>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)