Skip to content

Commit 87ef56f

Browse files
committed
优化预览图
1 parent 5e49672 commit 87ef56f

File tree

4 files changed

+108
-75
lines changed

4 files changed

+108
-75
lines changed

SwiftPamphletApp.xcodeproj/project.pbxproj

+8-4
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
086A5F4D2744EF3B00FECE02 /* guide-features.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F492744EF3B00FECE02 /* guide-features.json */; };
184184
086A5F4F2744EF3B00FECE02 /* guide-syntax.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F4B2744EF3B00FECE02 /* guide-syntax.json */; };
185185
086A5F502744EF3B00FECE02 /* developers.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F4C2744EF3B00FECE02 /* developers.json */; };
186-
086A5F522744EF4C00FECE02 /* ViewComponet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F512744EF4C00FECE02 /* ViewComponet.swift */; };
186+
086A5F522744EF4C00FECE02 /* ViewComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F512744EF4C00FECE02 /* ViewComponent.swift */; };
187187
086A5F58274CB8B500FECE02 /* guide-subject.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F57274CB8B500FECE02 /* guide-subject.json */; };
188188
086A5F5A274CC17700FECE02 /* lib-Combine.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F59274CC17700FECE02 /* lib-Combine.json */; };
189189
086A5F5D274CCAEC00FECE02 /* lib-Concurrency.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F5B274CCAEC00FECE02 /* lib-Concurrency.json */; };
@@ -268,6 +268,7 @@
268268
08F14B3E2BBDA3EA005B46CC /* NukeExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3D2BBDA3EA005B46CC /* NukeExtensions */; };
269269
08F14B402BBDA3EA005B46CC /* NukeUI in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3F2BBDA3EA005B46CC /* NukeUI */; };
270270
08F14B422BBDA3EA005B46CC /* NukeVideo in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B412BBDA3EA005B46CC /* NukeVideo */; };
271+
08F14B442BBE2865005B46CC /* ViewComponentImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F14B432BBE2865005B46CC /* ViewComponentImage.swift */; };
271272
08F1AB612BA0821900AEA0CA /* CodeEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 08F1AB602BA0821900AEA0CA /* CodeEditor */; };
272273
08F4BE6028609D8700733F12 /* PlayCharts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE5F28609D8600733F12 /* PlayCharts.swift */; };
273274
08F4BE6228616DC100733F12 /* PlayWeatherKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE6128616DC100733F12 /* PlayWeatherKit.swift */; };
@@ -454,7 +455,7 @@
454455
086A5F492744EF3B00FECE02 /* guide-features.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "guide-features.json"; sourceTree = "<group>"; };
455456
086A5F4B2744EF3B00FECE02 /* guide-syntax.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "guide-syntax.json"; sourceTree = "<group>"; };
456457
086A5F4C2744EF3B00FECE02 /* developers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = developers.json; sourceTree = "<group>"; };
457-
086A5F512744EF4C00FECE02 /* ViewComponet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewComponet.swift; sourceTree = "<group>"; };
458+
086A5F512744EF4C00FECE02 /* ViewComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewComponent.swift; sourceTree = "<group>"; };
458459
086A5F57274CB8B500FECE02 /* guide-subject.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "guide-subject.json"; sourceTree = "<group>"; };
459460
086A5F59274CC17700FECE02 /* lib-Combine.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "lib-Combine.json"; sourceTree = "<group>"; };
460461
086A5F5B274CCAEC00FECE02 /* lib-Concurrency.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "lib-Concurrency.json"; sourceTree = "<group>"; };
@@ -532,6 +533,7 @@
532533
08CD61FC27758B8A008C0935 /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = "<group>"; };
533534
08CD61FD27758B8A008C0935 /* Token.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = "<group>"; };
534535
08ED801B2B9D1EEC0069B7EC /* SettingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingView.swift; sourceTree = "<group>"; };
536+
08F14B432BBE2865005B46CC /* ViewComponentImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewComponentImage.swift; sourceTree = "<group>"; };
535537
08F4BE5F28609D8600733F12 /* PlayCharts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayCharts.swift; sourceTree = "<group>"; };
536538
08F4BE6128616DC100733F12 /* PlayWeatherKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayWeatherKit.swift; sourceTree = "<group>"; };
537539
08F51BC427A374A500693AB6 /* footer_js.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = footer_js.html; sourceTree = "<group>"; };
@@ -1205,7 +1207,8 @@
12051207
086A5F482744EF2B00FECE02 /* ViewComponet */ = {
12061208
isa = PBXGroup;
12071209
children = (
1208-
086A5F512744EF4C00FECE02 /* ViewComponet.swift */,
1210+
086A5F512744EF4C00FECE02 /* ViewComponent.swift */,
1211+
08F14B432BBE2865005B46CC /* ViewComponentImage.swift */,
12091212
0805F4952BAAABEA0008BB52 /* ViewStyle.swift */,
12101213
);
12111214
path = ViewComponet;
@@ -1688,6 +1691,7 @@
16881691
08BE634627C48C5B002BC6A8 /* PlayToggleView.swift in Sources */,
16891692
08448F41279AC1A900B61353 /* PlayMacOS.swift in Sources */,
16901693
0896FB9027BA39B100676B7F /* PlayButtonView.swift in Sources */,
1694+
08F14B442BBE2865005B46CC /* ViewComponentImage.swift in Sources */,
16911695
3AE0D59C2BAB0BD800D6D925 /* DeveloperModel.swift in Sources */,
16921696
08BE635A27C6575C002BC6A8 /* PlayGroupBoxView.swift in Sources */,
16931697
08BF26D8276B5D7F0064DDAC /* ExploreRepoListView.swift in Sources */,
@@ -1751,7 +1755,7 @@
17511755
083554E327572BB60095E0EE /* AppVM.swift in Sources */,
17521756
08BE637627CCAEA1002BC6A8 /* PlaySuperposedLayerView.swift in Sources */,
17531757
086A5F3A2744EDA500FECE02 /* APIRequest.swift in Sources */,
1754-
086A5F522744EF4C00FECE02 /* ViewComponet.swift in Sources */,
1758+
086A5F522744EF4C00FECE02 /* ViewComponent.swift in Sources */,
17551759
086A5F672754C27900FECE02 /* DBHandler.swift in Sources */,
17561760
0805F4962BAAABEA0008BB52 /* ViewStyle.swift in Sources */,
17571761
08BE637927CDCFD9002BC6A8 /* DataSortingListView.swift in Sources */,

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+18-6
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,26 @@ struct EditInfoView: View {
309309
do {
310310
let imgs = try soup.select("img").array()
311311
if imgs.count > 0 {
312-
let imgUrl = try imgs.randomElement()?.attr("src")
313-
if let okImgUrl = imgUrl {
314-
imageUrl = urlWithSchemeAndHost(url: url, urlStr: okImgUrl)
315-
}
312+
316313
for elm in imgs {
317-
let elmUrl = try elm.attr("src")
318-
imageUrls.append(urlWithSchemeAndHost(url: url, urlStr: elmUrl))
314+
if let elmUrl = try? elm.attr("src") {
315+
if elmUrl.isEmpty == false {
316+
imageUrls.append(urlWithSchemeAndHost(url: url, urlStr: elmUrl))
317+
}
318+
}
319319
}
320+
var imgUrl:String?
321+
if imageUrls.count > 0 {
322+
if imageUrls.count > 3 {
323+
imgUrl = imageUrls.randomElement()
324+
} else {
325+
imgUrl = imageUrls.first
326+
}
327+
if let okImgUrl = imgUrl {
328+
imageUrl = urlWithSchemeAndHost(url: url, urlStr: okImgUrl)
329+
}
330+
}
331+
320332
}
321333
} catch {}
322334

SwiftPamphletApp/ViewComponet/ViewComponet.swift SwiftPamphletApp/ViewComponet/ViewComponent.swift

+1-65
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import SwiftUI
99
import WebKit
1010
import MarkdownUI
11-
import Nuke
12-
import NukeUI
11+
1312

1413
// MARK: - 大纲
1514
struct SPOutlineListView<D, Content>: View where D: RandomAccessCollection, D.Element: Identifiable, Content: View {
@@ -295,70 +294,7 @@ struct FixAwfulPerformanceStyle: ButtonStyle {
295294
}
296295
}
297296

298-
// 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-
329-
struct AsyncImageWithPlaceholder: View {
330-
enum Size {
331-
case tinySize, smallSize,normalSize, bigSize
332297

333-
var v: CGFloat {
334-
switch self {
335-
case .tinySize:
336-
return 20
337-
case .smallSize:
338-
return 40
339-
case .normalSize:
340-
return 60
341-
case .bigSize:
342-
return 100
343-
}
344-
}
345-
}
346-
var size: Size
347-
var url: String
348-
var body: some View {
349-
AsyncImage(url: URL(string: url), content: { image in
350-
image
351-
.resizable()
352-
.aspectRatio(contentMode: .fit)
353-
.frame(width: size.v, height: size.v)
354-
.cornerRadius(5)
355-
},
356-
placeholder: {
357-
Image(systemName: "person")
358-
.frame(width: size.v, height: size.v)
359-
})
360-
}
361-
}
362298

363299
// MARK: - 跳到 Github 网站
364300
struct ButtonGoGitHubWeb: View {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// ViewComponentImage.swift
3+
// SwiftPamphletApp
4+
//
5+
// Created by Ming Dai on 2024/4/4.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
import Nuke
11+
import NukeUI
12+
13+
// MARK: - 图片
14+
struct NukeImage: View {
15+
private let pipeline = ImagePipeline {
16+
$0.dataLoader = {
17+
let config = DataLoader.defaultConfiguration
18+
config.urlCache = DataLoader.sharedUrlCache
19+
return DataLoader(configuration: config)
20+
}()
21+
}
22+
var width: CGFloat? = nil
23+
var height: CGFloat? = nil
24+
var url: String
25+
var contentModel: ContentMode = .fit
26+
var body: some View {
27+
LazyImage(url: URL(string: url)) { state in
28+
if let image = state.image {
29+
image
30+
.resizable()
31+
.aspectRatio(contentMode: contentModel)
32+
} else if state.isLoading == false {
33+
ZStack {
34+
Color.gray.opacity(0.2) // Placeholder
35+
Text(url)
36+
.font(.footnote)
37+
.foregroundColor(light: .secondary, dark: .secondary)
38+
}
39+
} else {
40+
ProgressView()
41+
}
42+
}
43+
.pipeline(pipeline)
44+
.frame(width: width, height: height)
45+
.cornerRadius(5)
46+
}
47+
}
48+
49+
struct AsyncImageWithPlaceholder: View {
50+
enum Size {
51+
case tinySize, smallSize,normalSize, bigSize
52+
53+
var v: CGFloat {
54+
switch self {
55+
case .tinySize:
56+
return 20
57+
case .smallSize:
58+
return 40
59+
case .normalSize:
60+
return 60
61+
case .bigSize:
62+
return 100
63+
}
64+
}
65+
}
66+
var size: Size
67+
var url: String
68+
var body: some View {
69+
AsyncImage(url: URL(string: url), content: { image in
70+
image
71+
.resizable()
72+
.aspectRatio(contentMode: .fit)
73+
.frame(width: size.v, height: size.v)
74+
.cornerRadius(5)
75+
},
76+
placeholder: {
77+
Image(systemName: "person")
78+
.frame(width: size.v, height: size.v)
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)