Skip to content

Commit

Permalink
优化下图片链接处理
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Apr 1, 2024
1 parent e356729 commit f8f1db3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
20 changes: 20 additions & 0 deletions SwiftPamphletApp/Core/FundationFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ func validHTTPUrlStrFromUrlStr(urlStr: String) -> String {
return httpsPrefix + urlStr
}

// 网页的相对地址转绝对地址
func urlWithSchemeAndHost(url: URL, urlStr: String) -> String {
var schemeStr = ""
var hostStr = ""
if let scheme = url.scheme, let host = url.host {
schemeStr = scheme
hostStr = host
}

if urlStr.hasPrefix("http") {
return urlStr
} else {
var slash = ""
if urlStr.hasPrefix("/") == false {
slash = "/"
}
return "\(schemeStr)://\(hostStr)\(slash)\(urlStr)"
}
}

// MARK: - 文件 - 沙盒
// 获取沙盒Document目录路径
func getDocumentsDirectory() -> URL {
Expand Down
22 changes: 4 additions & 18 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct EditInfoView: View {
}
VStack {
HStack {
PhotosPicker(selection: $selectedPhotos, matching: .images) {
PhotosPicker(selection: $selectedPhotos, matching: .not(.videos)) {
Label("选择照片图片", systemImage: "photo.on.rectangle.angled")
}
.onChange(of: selectedPhotos) { oldValue, newValue in
Expand Down Expand Up @@ -315,31 +315,17 @@ struct EditInfoView: View {
var imageUrls = [String]()

// 获取图集
var schemeStr = ""
var hostStr = ""
if let scheme = url.scheme, let host = url.host {
schemeStr = scheme
hostStr = host
}

do {
let imgs = try soup.select("img").array()
if imgs.count > 0 {
let imgUrl = try imgs.randomElement()?.attr("src")
if let okImgUrl = imgUrl {
if okImgUrl.hasPrefix("http") {
imageUrl = okImgUrl
} else {
imageUrl = "\(schemeStr)://\(hostStr)/\(okImgUrl)"
}
imageUrl = urlWithSchemeAndHost(url: url, urlStr: okImgUrl)
}
for elm in imgs {
let elmUrl = try elm.attr("src")
if elmUrl.hasPrefix("http") {
imageUrls.append(elmUrl)
} else {
imageUrls.append("\(schemeStr)://\(hostStr)/\(elmUrl)")
}

imageUrls.append(urlWithSchemeAndHost(url: url, urlStr: elmUrl))
}
}
} catch {}
Expand Down

0 comments on commit f8f1db3

Please sign in to comment.