Skip to content

Commit 5e49672

Browse files
committed
use nuke for image
1 parent a8e5e72 commit 5e49672

File tree

9 files changed

+88
-42
lines changed

9 files changed

+88
-42
lines changed

SwiftPamphletApp.xcodeproj/project.pbxproj

+41
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@
264264
08D107BD278826BB007B7009 /* HTMLEntities in Frameworks */ = {isa = PBXBuildFile; productRef = 08D107BC278826BB007B7009 /* HTMLEntities */; };
265265
08ED80162B9C54DE0069B7EC /* SMNetwork in Frameworks */ = {isa = PBXBuildFile; productRef = 08ED80152B9C54DE0069B7EC /* SMNetwork */; };
266266
08ED801C2B9D1EEC0069B7EC /* SettingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08ED801B2B9D1EEC0069B7EC /* SettingView.swift */; };
267+
08F14B3C2BBDA3EA005B46CC /* Nuke in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3B2BBDA3EA005B46CC /* Nuke */; };
268+
08F14B3E2BBDA3EA005B46CC /* NukeExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3D2BBDA3EA005B46CC /* NukeExtensions */; };
269+
08F14B402BBDA3EA005B46CC /* NukeUI in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3F2BBDA3EA005B46CC /* NukeUI */; };
270+
08F14B422BBDA3EA005B46CC /* NukeVideo in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B412BBDA3EA005B46CC /* NukeVideo */; };
267271
08F1AB612BA0821900AEA0CA /* CodeEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 08F1AB602BA0821900AEA0CA /* CodeEditor */; };
268272
08F4BE6028609D8700733F12 /* PlayCharts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE5F28609D8600733F12 /* PlayCharts.swift */; };
269273
08F4BE6228616DC100733F12 /* PlayWeatherKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE6128616DC100733F12 /* PlayWeatherKit.swift */; };
@@ -544,12 +548,16 @@
544548
08448F092796A83D00B61353 /* SwiftDate in Frameworks */,
545549
08BF26D02768A59A0064DDAC /* SQLite in Frameworks */,
546550
08448F4E279E8CA400B61353 /* Ink in Frameworks */,
551+
08F14B402BBDA3EA005B46CC /* NukeUI in Frameworks */,
552+
08F14B422BBDA3EA005B46CC /* NukeVideo in Frameworks */,
553+
08F14B3E2BBDA3EA005B46CC /* NukeExtensions in Frameworks */,
547554
08B9D5312BA3E08300CACCBE /* SwiftHTMLtoMarkdown in Frameworks */,
548555
08BF26D32768A5B40064DDAC /* MarkdownUI in Frameworks */,
549556
08F1AB612BA0821900AEA0CA /* CodeEditor in Frameworks */,
550557
08397E322B9F39C100DFDD02 /* SwiftSoup in Frameworks */,
551558
08ED80162B9C54DE0069B7EC /* SMNetwork in Frameworks */,
552559
08D107BD278826BB007B7009 /* HTMLEntities in Frameworks */,
560+
08F14B3C2BBDA3EA005B46CC /* Nuke in Frameworks */,
553561
);
554562
runOnlyForDeploymentPostprocessing = 0;
555563
};
@@ -1432,6 +1440,10 @@
14321440
08397E312B9F39C100DFDD02 /* SwiftSoup */,
14331441
08F1AB602BA0821900AEA0CA /* CodeEditor */,
14341442
08B9D5302BA3E08300CACCBE /* SwiftHTMLtoMarkdown */,
1443+
08F14B3B2BBDA3EA005B46CC /* Nuke */,
1444+
08F14B3D2BBDA3EA005B46CC /* NukeExtensions */,
1445+
08F14B3F2BBDA3EA005B46CC /* NukeUI */,
1446+
08F14B412BBDA3EA005B46CC /* NukeVideo */,
14351447
);
14361448
productName = SwiftPamphletApp;
14371449
productReference = 086A5F032744E88E00FECE02 /* 戴铭的开发小册子.app */;
@@ -1472,6 +1484,7 @@
14721484
08397E302B9F39C100DFDD02 /* XCRemoteSwiftPackageReference "SwiftSoup" */,
14731485
08F1AB5F2BA0821900AEA0CA /* XCRemoteSwiftPackageReference "CodeEditor" */,
14741486
08B9D52F2BA3E08300CACCBE /* XCRemoteSwiftPackageReference "SwiftHTMLToMarkdown" */,
1487+
08F14B3A2BBDA3EA005B46CC /* XCRemoteSwiftPackageReference "Nuke" */,
14751488
);
14761489
productRefGroup = 086A5F042744E88E00FECE02 /* Products */;
14771490
projectDirPath = "";
@@ -2042,6 +2055,14 @@
20422055
minimumVersion = 4.0.0;
20432056
};
20442057
};
2058+
08F14B3A2BBDA3EA005B46CC /* XCRemoteSwiftPackageReference "Nuke" */ = {
2059+
isa = XCRemoteSwiftPackageReference;
2060+
repositoryURL = "https://github.com/kean/Nuke.git";
2061+
requirement = {
2062+
kind = upToNextMajorVersion;
2063+
minimumVersion = 12.5.0;
2064+
};
2065+
};
20452066
08F1AB5F2BA0821900AEA0CA /* XCRemoteSwiftPackageReference "CodeEditor" */ = {
20462067
isa = XCRemoteSwiftPackageReference;
20472068
repositoryURL = "https://github.com/ZeeZide/CodeEditor.git";
@@ -2092,6 +2113,26 @@
20922113
isa = XCSwiftPackageProductDependency;
20932114
productName = SMNetwork;
20942115
};
2116+
08F14B3B2BBDA3EA005B46CC /* Nuke */ = {
2117+
isa = XCSwiftPackageProductDependency;
2118+
package = 08F14B3A2BBDA3EA005B46CC /* XCRemoteSwiftPackageReference "Nuke" */;
2119+
productName = Nuke;
2120+
};
2121+
08F14B3D2BBDA3EA005B46CC /* NukeExtensions */ = {
2122+
isa = XCSwiftPackageProductDependency;
2123+
package = 08F14B3A2BBDA3EA005B46CC /* XCRemoteSwiftPackageReference "Nuke" */;
2124+
productName = NukeExtensions;
2125+
};
2126+
08F14B3F2BBDA3EA005B46CC /* NukeUI */ = {
2127+
isa = XCSwiftPackageProductDependency;
2128+
package = 08F14B3A2BBDA3EA005B46CC /* XCRemoteSwiftPackageReference "Nuke" */;
2129+
productName = NukeUI;
2130+
};
2131+
08F14B412BBDA3EA005B46CC /* NukeVideo */ = {
2132+
isa = XCSwiftPackageProductDependency;
2133+
package = 08F14B3A2BBDA3EA005B46CC /* XCRemoteSwiftPackageReference "Nuke" */;
2134+
productName = NukeVideo;
2135+
};
20952136
08F1AB602BA0821900AEA0CA /* CodeEditor */ = {
20962137
isa = XCSwiftPackageProductDependency;
20972138
package = 08F1AB5F2BA0821900AEA0CA /* XCRemoteSwiftPackageReference "CodeEditor" */;

SwiftPamphletApp/GitHubAPI/DetailView/IssueView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct IssueView: View {
3333
Text(" \(howLongFromNow(timeStr:vm.issue.updatedAt))更新过").font(.footnote)
3434
} else {
3535
HStack {
36-
AsyncImageWithPlaceholder(size: .smallSize, url: vm.issue.user.avatarUrl)
36+
NukeImage(width: 40, height:40, url: vm.issue.user.avatarUrl)
3737
VStack(alignment:.leading) {
3838
NavigationLink(destination: UserView(vm: UserVM(userName: vm.issue.user.login)), label: {
3939
Text(vm.issue.user.login)
@@ -63,7 +63,7 @@ struct IssueView: View {
6363
VStack(alignment: .leading) {
6464
GitHubApiTimeView(timeStr: comment.updatedAt)
6565
HStack {
66-
AsyncImageWithPlaceholder(size: .smallSize, url: comment.user.avatarUrl)
66+
NukeImage(width: 40, height: 40, url: comment.user.avatarUrl)
6767
ButtonGoGitHubWeb(url: comment.user.login, text: comment.user.login, ignoreHost: true)
6868

6969
Text(comment.authorAssociation)

SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct RepoView: View {
5454

5555
HStack {
5656
Text("作者:")
57-
AsyncImageWithPlaceholder(size: .smallSize, url: vm.repo.owner.avatarUrl)
57+
NukeImage(width: 40, height: 40, url: vm.repo.owner.avatarUrl)
5858
ButtonGoGitHubWeb(url: vm.repo.owner.login, text: vm.repo.owner.login, ignoreHost: true)
5959
}
6060
} // end VStack
@@ -218,7 +218,7 @@ struct RepoCommitLabelView: View {
218218
Image(systemName: "envelope.badge.fill")
219219
}
220220
if commit.author != nil {
221-
AsyncImageWithPlaceholder(size: .tinySize, url: commit.author?.avatarUrl ?? "")
221+
NukeImage(width: 20, height: 20, url: commit.author?.avatarUrl ?? "")
222222
ButtonGoGitHubWeb(url: commit.author?.login ?? "", text: commit.author?.login ?? "", ignoreHost: true, bold: true)
223223

224224
} else {
@@ -244,7 +244,7 @@ struct IssueLabelView: View {
244244
.font(.footnote)
245245
}
246246
HStack {
247-
AsyncImageWithPlaceholder(size: .tinySize, url: issue.user.avatarUrl)
247+
NukeImage(width: 20, height: 20, url: issue.user.avatarUrl)
248248
ButtonGoGitHubWeb(url: issue.user.login, text: issue.user.login, ignoreHost: true)
249249
}
250250
MarkdownView(s: issue.body ?? "")
@@ -258,7 +258,7 @@ struct IssueEventLabelView: View {
258258
VStack(alignment: .leading, spacing: 5) {
259259
GitHubApiTimeView(timeStr: issueEvent.createdAt)
260260
HStack {
261-
AsyncImageWithPlaceholder(size: .tinySize, url: issueEvent.actor.avatarUrl)
261+
NukeImage(width: 20, height: 20, url: issueEvent.actor.avatarUrl)
262262
ButtonGoGitHubWeb(url: issueEvent.actor.login, text: issueEvent.actor.login, ignoreHost: true)
263263
Text(issueEvent.event)
264264
.foregroundColor(.secondary)
@@ -271,7 +271,7 @@ struct IssueEventLabelView: View {
271271
.font(.footnote)
272272
}
273273
HStack {
274-
AsyncImageWithPlaceholder(size: .tinySize, url: issueEvent.issue.user.avatarUrl)
274+
NukeImage(width: 20, height: 20, url: issueEvent.issue.user.avatarUrl)
275275
ButtonGoGitHubWeb(url: issueEvent.issue.user.login, text: issueEvent.issue.user.login, ignoreHost: true)
276276
}
277277
MarkdownView(s: issueEvent.issue.body ?? "")

SwiftPamphletApp/GitHubAPI/DetailView/UserView.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct UserView: View {
1818
HStack {
1919
VStack(alignment: .leading, spacing: 10) {
2020
HStack {
21-
AsyncImageWithPlaceholder(size: .normalSize, url: vm.user.avatarUrl)
21+
NukeImage(width: 60, height: 60, url: vm.user.avatarUrl)
2222
VStack(alignment: .leading, spacing: 5) {
2323
HStack {
2424
Text(vm.user.name ?? vm.user.login).font(.system(.title))
@@ -177,8 +177,7 @@ struct AUserEventLabel: View {
177177
}
178178

179179
if isShowActor == true {
180-
AsyncImageWithPlaceholder(size: .tinySize, url: event.actor.avatarUrl)
181-
180+
NukeImage(width: 20, height: 20, url: event.actor.avatarUrl)
182181
Text(event.actor.login).bold()
183182

184183
} // end if

SwiftPamphletApp/GitHubAPI/Developer/DeveloperListView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct DeveloperListView: View {
1717
List(selection: $selectDev) {
1818
ForEach(devs) { dev in
1919
HStack {
20-
AsyncImageWithPlaceholder(size: .smallSize, url: dev.avatar)
20+
NukeImage(width: 40, height: 40, url: dev.avatar)
2121
VStack {
2222
HStack {
2323
if dev.repoName.isEmpty {

SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct EditDeveloper: View {
7171

7272
HStack {
7373
Text("作者:")
74-
AsyncImageWithPlaceholder(size: .smallSize, url: vmRepo.repo.owner.avatarUrl)
74+
NukeImage(width:40, height: 40, url: vmRepo.repo.owner.avatarUrl)
7575
ButtonGoGitHubWeb(url: vmRepo.repo.owner.login, text: vmRepo.repo.owner.login, ignoreHost: true)
7676
}
7777
} // end VStack
@@ -142,7 +142,7 @@ struct EditDeveloper: View {
142142
HStack {
143143
VStack(alignment: .leading, spacing: 10) {
144144
HStack {
145-
AsyncImageWithPlaceholder(size: .normalSize, url: vm.user.avatarUrl)
145+
NukeImage(width:60, height: 60, url: vm.user.avatarUrl)
146146
VStack(alignment: .leading, spacing: 5) {
147147
HStack {
148148
Text(vm.user.name ?? vm.user.login).font(.system(.title))

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+2-18
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,7 @@ struct EditInfoView: View {
188188
}
189189
}
190190
} else if img.url.isEmpty == false {
191-
AsyncImage(url: URL(string: img.url), content: { image in
192-
image
193-
.resizable()
194-
.scaledToFit()
195-
.cornerRadius(5)
196-
},
197-
placeholder: {
198-
Image(systemName: "photo.fill")
199-
})
191+
NukeImage(url: img.url)
200192
.contextMenu {
201193
Button {
202194
IOInfo.updateCoverImage(info: info, img: IOImg(url: img.url))
@@ -219,15 +211,7 @@ struct EditInfoView: View {
219211
if info.imageUrls.isEmpty == false {
220212
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150), spacing: 10)]) {
221213
ForEach(info.imageUrls, id:\.self) { img in
222-
AsyncImage(url: URL(string: img), content: { image in
223-
image
224-
.resizable()
225-
.aspectRatio(contentMode: .fit)
226-
.cornerRadius(5)
227-
},
228-
placeholder: {
229-
Image(systemName: "photo.fill")
230-
})
214+
NukeImage(url: img)
231215
.contextMenu {
232216
Button {
233217
IOInfo.updateCoverImage(info: info, img: IOImg(url: img))

SwiftPamphletApp/InfoOrganizer/Info/InfoRowView.swift

+1-11
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@ struct InfoRowView: View {
2323
HStack(alignment:.top) {
2424
if let coverImg = info.coverImage {
2525
if coverImg.url.isEmpty == false {
26-
AsyncImage(url: URL(string: coverImg.url), content: { image in
27-
image
28-
.resizable()
29-
.scaledToFill()
30-
.frame(width: 60, height: 60)
31-
.cornerRadius(5)
32-
},
33-
placeholder: {
34-
Image(systemName: "photo.fill")
35-
.frame(width: 60, height: 60)
36-
})
26+
NukeImage(width: 60, height: 60, url: coverImg.url, contentModel: .fill)
3727
} else if let imgData = coverImg.imgData {
3828
if let nsImage = NSImage(data: imgData) {
3929
Image(nsImage: nsImage)

SwiftPamphletApp/ViewComponet/ViewComponet.swift

+32
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import SwiftUI
99
import WebKit
1010
import MarkdownUI
11+
import Nuke
12+
import NukeUI
1113

1214
// MARK: - 大纲
1315
struct SPOutlineListView<D, Content>: View where D: RandomAccessCollection, D.Element: Identifiable, Content: View {
@@ -294,6 +296,36 @@ struct FixAwfulPerformanceStyle: ButtonStyle {
294296
}
295297

296298
// MARK: - 图片
299+
struct NukeImage: View {
300+
private let pipeline = ImagePipeline {
301+
$0.dataLoader = {
302+
let config = DataLoader.defaultConfiguration
303+
config.urlCache = DataLoader.sharedUrlCache
304+
return DataLoader(configuration: config)
305+
}()
306+
}
307+
var width: CGFloat? = nil
308+
var height: CGFloat? = nil
309+
var url: String
310+
var contentModel: ContentMode = .fit
311+
var body: some View {
312+
LazyImage(url: URL(string: url)) { state in
313+
if let image = state.image {
314+
image
315+
.resizable()
316+
.aspectRatio(contentMode: contentModel)
317+
.frame(width: width, height: height)
318+
.cornerRadius(5)
319+
} else {
320+
Color.gray.opacity(0.2) // Placeholder
321+
.frame(width: width, height: height)
322+
}
323+
}
324+
.pipeline(pipeline)
325+
326+
}
327+
}
328+
297329
struct AsyncImageWithPlaceholder: View {
298330
enum Size {
299331
case tinySize, smallSize,normalSize, bigSize

0 commit comments

Comments
 (0)