我从我的服务器获取一个图像URL,我想将其保存在文档目录中。但是在我的下面的代码中,我收到了一个错误。
这是我的文件URL路径 - file:///var/mobile/Containers/Data/Application/7AA1BEDE-DA10-4BD6-8115-06C9DCA53BF2/Documents/https://www.cocubes.com/images/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)
}
}
}
请在下面添加以下行:let filePath = documentsDirectory.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)
}
}
}
问题是urlString包含/
,FileManager试图将图像数据保存在不存在的文件夹中。您可以通过用/
替换_
字符的出现来解决这个问题。
let fileURL = documentsDirectory.appendingPathComponent((urlString as String).replacingOccurrences(of: "/", with: "_"))
另一种解决方案是首先创建一个文件夹。
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 {
// First create a directory
try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
// writes the image data to disk
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}