如何在文件夹

时间:2017-04-12 10:51:16

标签: ios swift

我想将图像保存到文档/图像/ AET / 1.jpg但到目前为止我只能保存到文档文件夹。

我写了代码

 let fileManager = FileManager.default

    let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("images")

    try! fileManager.createDirectory(atPath: paths, withIntermediateDirectories: true, attributes: nil)
    fileManager.changeCurrentDirectoryPath(paths)

    let image = imagePic// UIImage(named: "apple.jpg")
    print(paths)


    let paths3 = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("/\(chkString!).jpg")
    print("chkString=\(chkString!)")


    if !fileManager.fileExists(atPath: paths){
        try! fileManager.createDirectory(atPath: paths3, withIntermediateDirectories: true, attributes: nil)
    }else{
        print("Already dictionary created.")
    }
    let imageData = UIImageJPEGRepresentation(image, 0.5)


    // let pathToDatabase = paths3.appending("/\(chkString)")
    // let pathToDatabase =   paths3.appendingPathComponent("/\(chkString)")
    fileManager.createFile(atPath: paths3 as String, contents: imageData, attributes: nil)
    print("imagePath = \(paths3)")

3 个答案:

答案 0 :(得分:1)

主要问题是您无法创建传递文件路径的目录,包括文件名(/../AET/1.jpg)。您需要指定不带文件名的文件夹路径。

通常强烈建议使用与URL相关的API。

此示例使用与URL相关的API,更具描述性的变量名称和Data的API将图像数据写入磁盘。

let chkString : String? = "AET"

let fileManager = FileManager.default

do {
    let documentDirectoryURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let imagesFolderURL = documentDirectoryURL.appendingPathComponent("images/\(chkString!)")
    let imageURL = imagesFolderURL.appendingPathComponent("1.jpg")

    let image = imagePic// UIImage(named: "apple.jpg")

    if !fileManager.fileExists(atPath: imagesFolderURL.path){
        try fileManager.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)
    } else {
        print("Already dictionary created.")
    }
    let imageData = UIImageJPEGRepresentation(image, 0.5)
    try imageData!.write(to: imageURL)

    print("imagePath =", imageURL.path)
} catch {
    print(error)
}

答案 1 :(得分:0)

使用此:

let fileManager = FileManager.default

let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
print(documentPath)

let imageFolder = documentPath.appending("/images/AET")

if !fileManager.fileExists(atPath: imageFolder) {
    do{
         try fileManager.createDirectory(atPath: imageFolder, withIntermediateDirectories: true, attributes: nil)
            }
     catch (let error){
           print("Failed to create Directory: \(error.localizedDescription)")
     }
}

您应该创建" / images / AET" 等子文件夹。

答案 2 :(得分:0)

    let fileManager = FileManager.default
    let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let path = "\(documentPath[0])/images/AET"
    if !fileManager.fileExists(atPath: path) {
        try! fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
    } else {
        print("Directroy is already created")

    }

您可以打印路径并通过终端检查它将提供您想要的目录。