Skip to content

Commit 4db156d

Browse files
committed
浏览图片可点击放大
1 parent fb95ea6 commit 4db156d

File tree

1 file changed

+111
-67
lines changed

1 file changed

+111
-67
lines changed

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

Lines changed: 111 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ struct EditInfoView: View {
110110
}
111111

112112
// MARK: Image
113+
@State private var largeImageUrlStr = ""
114+
@State private var largeNSImage: NSImage? = nil
115+
113116
@MainActor
114117
@ViewBuilder
115118
func imagePickAndShowView() -> some View {
@@ -129,84 +132,119 @@ struct EditInfoView: View {
129132
}
130133
}
131134
}
132-
ScrollView {
133-
if let infoImgs = info.imgs {
134-
if infoImgs.count > 0 {
135-
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150),spacing: 10)]) {
136-
ForEach(Array(infoImgs.enumerated()), id:\.0) {i, img in
137-
VStack {
138-
if let data = img.imgData {
139-
if let nsImg = NSImage(data: data) {
140-
Image(nsImage: nsImg)
141-
.resizable()
142-
.scaledToFit()
143-
.cornerRadius(5)
144-
.contextMenu {
145-
Button {
146-
IOInfo.updateCoverImage(info: info, img: img)
147-
} label: {
148-
Label("设为封面图", image: "doc.text.image")
149-
}
150-
Button {
151-
info.imgs?.remove(at: i)
152-
IOImg.delete(img)
153-
} label: {
154-
Label("删除", image: "circle")
155-
}
135+
ZStack {
136+
ScrollView {
137+
if let infoImgs = info.imgs {
138+
if infoImgs.count > 0 {
139+
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150),spacing: 10)]) {
140+
ForEach(Array(infoImgs.enumerated()), id:\.0) {i, img in
141+
VStack {
142+
if let data = img.imgData {
143+
if let nsImg = NSImage(data: data) {
144+
Image(nsImage: nsImg)
145+
.resizable()
146+
.scaledToFit()
147+
.cornerRadius(5)
148+
.onTapGesture(perform: {
149+
largeNSImage = nsImg
150+
})
151+
.contextMenu {
152+
Button {
153+
IOInfo.updateCoverImage(info: info, img: img)
154+
} label: {
155+
Label("设为封面图", image: "doc.text.image")
156+
}
157+
Button {
158+
info.imgs?.remove(at: i)
159+
IOImg.delete(img)
160+
} label: {
161+
Label("删除", image: "circle")
162+
}
156163

157-
}
158-
}
159-
} else if img.url.isEmpty == false {
160-
NukeImage(url: img.url)
161-
.contextMenu {
162-
Button {
163-
IOInfo.updateCoverImage(info: info, img: IOImg(url: img.url))
164-
} label: {
165-
Label("设为封面图", image: "doc.text.image")
164+
}
166165
}
167-
Button {
168-
let p = NSPasteboard.general
169-
p.declareTypes([.string], owner: nil)
170-
p.setString(img.url, forType: .string)
171-
} label: {
172-
Label("复制图片链接", image: "circle")
166+
} else if img.url.isEmpty == false {
167+
NukeImage(url: img.url)
168+
.contextMenu {
169+
Button {
170+
IOInfo.updateCoverImage(info: info, img: IOImg(url: img.url))
171+
} label: {
172+
Label("设为封面图", image: "doc.text.image")
173+
}
174+
Button {
175+
let p = NSPasteboard.general
176+
p.declareTypes([.string], owner: nil)
177+
p.setString(img.url, forType: .string)
178+
} label: {
179+
Label("复制图片链接", image: "circle")
180+
}
181+
Button {
182+
info.imgs?.remove(at: i)
183+
IOImg.delete(img)
184+
} label: {
185+
Label("删除", image: "circle")
186+
}
173187
}
174-
Button {
175-
info.imgs?.remove(at: i)
176-
IOImg.delete(img)
177-
} label: {
178-
Label("删除", image: "circle")
188+
.onTapGesture {
189+
largeImageUrlStr = img.url
179190
}
180191
}
192+
} // end VStack
193+
} // end ForEach
194+
} // end LazyVGrid
195+
}
196+
} // end if let
197+
if info.imageUrls.isEmpty == false {
198+
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150), spacing: 10)]) {
199+
ForEach(info.imageUrls, id:\.self) { img in
200+
NukeImage(url: img)
201+
.contextMenu {
202+
Button {
203+
IOInfo.updateCoverImage(info: info, img: IOImg(url: img))
204+
} label: {
205+
Label("设为封面图", image: "doc.text.image")
206+
}
207+
Button {
208+
let p = NSPasteboard.general
209+
p.declareTypes([.string], owner: nil)
210+
p.setString(img, forType: .string)
211+
} label: {
212+
Label("复制图片链接", image: "circle")
181213
}
182-
} // end VStack
183-
} // end ForEach
184-
} // end LazyVGrid
185-
}
186-
} // end if let
187-
if info.imageUrls.isEmpty == false {
188-
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150), spacing: 10)]) {
189-
ForEach(info.imageUrls, id:\.self) { img in
190-
NukeImage(url: img)
191-
.contextMenu {
192-
Button {
193-
IOInfo.updateCoverImage(info: info, img: IOImg(url: img))
194-
} label: {
195-
Label("设为封面图", image: "doc.text.image")
196214
}
197-
Button {
198-
let p = NSPasteboard.general
199-
p.declareTypes([.string], owner: nil)
200-
p.setString(img, forType: .string)
201-
} label: {
202-
Label("复制图片链接", image: "circle")
215+
.onTapGesture {
216+
withAnimation {
217+
largeImageUrlStr = img
218+
}
203219
}
204220
}
205221
}
206222
}
223+
} // end scrollView
224+
if largeImageUrlStr.isEmpty == false {
225+
NukeImage(url: largeImageUrlStr)
226+
.onTapGesture {
227+
withAnimation {
228+
largeImageUrlStr = ""
229+
}
230+
}
231+
}
232+
if largeNSImage != nil {
233+
Image(nsImage: largeNSImage!)
234+
.resizable()
235+
.scaledToFit()
236+
.onTapGesture {
237+
withAnimation {
238+
largeNSImage = nil
239+
}
240+
}
207241
}
208242
}
209243
}
244+
.onChange(of: info, { oldValue, newValue in
245+
largeImageUrlStr = ""
246+
largeNSImage = nil
247+
})
210248
.padding(10)
211249
.tabItem { Label("图集", systemImage: "circle")}
212250
.tag(5)
@@ -306,7 +344,13 @@ struct EditInfoView: View {
306344
func urlInputView() -> some View {
307345
HStack {
308346
TextField("地址:", text: $info.url, prompt: Text("输入或粘贴 url,例如 https://www.starming.com")).rounded()
309-
.onSubmit {
347+
// .onSubmit {
348+
// Task.detached(priority: .background, operation: {
349+
//
350+
// })
351+
// }
352+
if info.url.isEmpty == false {
353+
Button {
310354
Task {
311355
// MARK: 获取 Web 内容
312356
info.name = "获取标题中......"
@@ -321,8 +365,9 @@ struct EditInfoView: View {
321365
}
322366
}
323367
}
368+
} label: {
369+
Text("解析")
324370
}
325-
if info.url.isEmpty == false {
326371
Button {
327372
gotoWebBrowser(urlStr: info.url)
328373
} label: {
@@ -454,7 +499,6 @@ struct EditInfoView: View {
454499
var imageUrls = [String]()
455500

456501
// 获取图集
457-
458502
do {
459503
let imgs = try soup.select("img").array()
460504
if imgs.count > 0 {

0 commit comments

Comments
 (0)