【问题标题】:delete a file using swift in ios在ios中使用swift删除文件
【发布时间】:2017-02-04 14:18:46
【问题描述】:
func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

很简单,我使用该函数获取我的文件名。它现在正确返回,我如何使用 Swift 3 删除这个文件? 我怎样才能像UIImageJPEGRepresentation一样读回图像?

let image_data = UIImageJPEGRepresentation(self.commons.correctlyOrientedImage(image: self.imageSelected.image!),0.5)

我做了跟踪,但它不起作用

let filename = getDocumentsDirectory().appendingPathComponent("l.png")   let

fileManager = FileManager.default var filePath = 文件名 // 检查文件是否存在

                if fileManager.fileExists(atPath: filePath.absoluteString) {
                    // Delete file
                    try fileManager.removeItem(atPath: filePath.absoluteString)
                } else {
                    print("File does not exist")
                }

我这样保存文件

let filename =    getDocumentsDirectory().appendingPathComponent("COPY111.PNG")
        try? image_data?.write(to: filename)

但我无法从下面提供的答案中删除它

【问题讨论】:

    标签: ios file swift3


    【解决方案1】:

    删除文件有几行:

    do {
        try FileManager.default.removeItem(at: fileName)
    } catch let error as NSError {
        print("Error: \(error.domain)")
    }
    

    下面是设置它为 imageView 的代码:

    if let image = UIImage(contentsOfFile: fileName) {
       imageView.image = image
    }
    

    【讨论】:

      【解决方案2】:

      试试这个。希望它会工作。要删除文件

          let fileNameToDelete = "myFileName.txt"
          var filePath = ""
      
          // Fine documents directory on device
          let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
          let documentDirectory = paths[0]
          filePath = documentDirectory.appendingFormat("/" + fileNameToDelete)
      
          do {
              let fileManager = FileManager.default
      
              // Check if file exists
              if fileManager.fileExists(atPath: filePath) {
                  // Delete file
                  try fileManager.removeItem(atPath: filePath)
              } else {
                  print("File does not exist")
              }
      
          }
          catch let error as NSError {
              print("An error took place: \(error)")
          }
      

      来源:http://swiftdeveloperblog.com/code-examples/delete-file-example-in-swift/

      对于图片加载

      let imageData = UIImageJPEGRepresentation(image)!  
      let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)  
      let imageURL = docDir.appendingPathComponent("tmp.jpeg")  
      try! imageData.write(to: imageURL)  
      
      let newImage = UIImage(contentsOfFile: imageURL.path)! 
      

      【讨论】:

      • 我编辑了我的第一篇文章,我做了你的解决方案,但我得到文件不存在
      • 我得到文件路径,但删除时说文件不存在 filePath /var/mobile/Containers/Data/Application/D5FA0123-65A2-470A-A817-8871F984CE90/Documents/profile-FFCEBEA9-2F8D -49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png filePath 文件不存在
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      相关资源
      最近更新 更多