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

时间:2019-03-12 06:54:28

标签: ios swift debugging nsfilemanager

我从服务器获取图像URL,该图像URL要保存在documents目录中。 但是在下面的代码中,我遇到了错误。

这是我的文件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)
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请在下面添加此行:           让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)
                }
        }
    }

答案 1 :(得分:0)

问题是因为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)
        }
    }
}