简体   繁体   中英

Error in saving image in documents directory

I am getting an image URL from my server which I want to save in documents directory. But in my following code, I am getting an error.

here is my file URL Path - file:///var/mobile/Containers/Data/Application/7AA1BEDE-DA10-4BD6-8115-06C9DCA53BF2/Documents/https://www.cocubes.com/images/getimage.aspx%3Ft=l&id=602

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

The '?' is getting converted to %3F, because of which I guess(not sure) I am not able to go to the desired path. How to solve this problem??

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)
        }
    }
}

PLease add this line below : 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)
                }
        }
    }

The problem is as the urlString contains / the FileManager is trying to save the data of the image inside a folder that does not exist. You can fix that by replacing the occurrences of the / character with _ .

let fileURL = documentsDirectory.appendingPathComponent((urlString as String).replacingOccurrences(of: "/", with: "_"))

Another solution is to create a folder first.

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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM