[英]How to change image filePath programmatically?
This is an example of image location I need to change:这是我需要更改的图像位置示例:
<img src="file:///Users/User/Library/Developer/CoreSimulator/Devices/B343D95A-92FD-42E4-B10E-BF85210FBC33/data/Containers/Data/Application/662DCB20-3D51-467C-9968-8CA463304B46/Documents/data/a1a1a1/xyz.gif"
This is what I want to remove and put something else there: data/a1a1a1/
这就是我想要删除并放置其他内容的内容:
data/a1a1a1/
With the function below I have successfully removed this part: data/
and put what I wanted there.使用下面的功能,我成功地删除了这部分:
data/
并将我想要的放在那里。 Works fine because folder name
data never changes.工作正常,因为文件夹
name
数据永远不会改变。
Problem I want to remove this part now : a1a1a1/
but problem is that a1a1a1
can be anything, for example it can be aef54e3a
...问题我现在想删除这部分:
a1a1a1/
但问题是a1a1a1
可以是任何东西,例如它可以是aef54e3a
...
Does anyone have an idea how to remove that part of the html string?有谁知道如何删除 html 字符串的那部分?
Function :功能 :
func findSrcs(_ content: String) -> String {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
let pattern = #"(?<=src=")[^"]+"#
var x = content //content is a highlighted string I put above
var startIndex = content.startIndex
while let range = content[startIndex...].range(of: pattern, options: .regularExpression) {
startIndex = range.upperBound
let c = content.replacingOccurrences(of: "data/", with: "\(documentsDirectory)", options: .literal, range: nil)
//let d = c.replacingOccurrences(of: "7216444/", with: "", options: .literal, range: nil)
x = c
}
return x
}
Function call:函数调用:
You can change the code inside the while loop to this:您可以将 while 循环内的代码更改为:
let url = URL(fileURLWithPath: String(content[range]))
x = documentsDirectory.appendingPathComponent(url.lastPathComponent)
Though I'm not sure if you want just a single x returned (and then why use while
?) or an array of of paths (and then the function should return [String]
).虽然我不确定您是否只想要返回一个 x (然后为什么使用
while
?)或一组路径(然后函数应该返回[String]
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.