gpt4 book ai didi

ios - 在文档目录中保存图像时出错

转载 作者:行者123 更新时间:2023-11-29 05:56:22 24 4
gpt4 key购买 nike

我从我的服务器获取一个图像 URL,我想将其保存在文档目录中。但在我的以下代码中,我收到错误。

这是我的文件 URL 路径 - file:///var/mobile/Containers/Data/Application/7AA1BEDE-DA10-4BD6-8115-06C9DCA53BF2/Documents/https://www.cocubes.com/图片/getimage.aspx%3Ft=l&id=602

文件 URL.path = /var/mobile/Containers/Data/Application/7AA1BEDE-DA10-4BD6-8115-06C9DCA53BF2/Documents/https://www.cocubes.com/images/getimage.aspx? t=l&id=602

“?”正在转换为 %3F,因此我猜(不确定)我无法转到所需的路径。怎么解决这个问题?

static func saveImageInDocsDir(image: UIImage,urlString: NSString) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// choose a name for your image
//let fileName = "image.jpg"
// create the destination file url to save your image
let fileURL = documentsDirectory.appendingPathComponent(urlString as String)
print(fileURL)
print(fileURL.path)
// get your UIImage jpeg data representation and check if the destination file url already exists
if let data = image.jpegData(compressionQuality: 1.0),
!FileManager.default.fileExists(atPath: fileURL.path) {
do {
// writes the image data to disk
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}

最佳答案

请在下面添加这一行: 让 filePath = filesDirectory.appendingPathComponent(fileURL)

 static func saveImageInDocsDir(image: UIImage,urlString: NSString) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// choose a name for your image
//let fileName = "image.jpg"
// create the destination file url to save your image
let fileURL = documentsDirectory.appendingPathComponent(urlString as String)
print(fileURL)
print(fileURL.path)
// get your UIImage jpeg data representation and check if the destination file url already exists
let filePath = documentsDirectory.appendingPathComponent(fileURL)
if let data = image.jpegData(compressionQuality: 1.0),
!FileManager.default.fileExists(atPath: filePath.path) {
do {
// writes the image data to disk
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}

关于ios - 在文档目录中保存图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55115693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com