在iOS中使用Swift删除文件 [英] delete a file using swift in ios
本文介绍了在iOS中使用Swift删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
很简单,我使用该函数获取文件名,并且它现在可以正确返回,如何使用Swift 3删除该文件?以及如何像 UIImageJPEGRepresentation
一样读回图像?
So simply, I get my filename using that function.And it returns correctly for now, how can I delete this file using Swift 3?
And How can I read the image back like UIImageJPEGRepresentation
?
let image_data = UIImageJPEGRepresentation(self.commons.correctlyOrientedImage(image: self.imageSelected.image!),0.5)
我照做了,但是没用
let filename = getDocumentsDirectory().appendingPathComponent("l.png") let
fileManager = FileManager.defaultvar filePath =文件名//检查文件是否存在
fileManager = FileManager.default var filePath = filename // Check if file exists
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)
但是我无法从下面提供的答案中将其删除
But I cannot remove it from answers provided below
推荐答案
以下是用于删除文件的行:
There're lines for deleting a file:
do {
try FileManager.default.removeItem(at: fileName)
} catch let error as NSError {
print("Error: \(error.domain)")
}
这是将其设置为imageView的代码:
And here's code for setting it to imageView:
if let image = UIImage(contentsOfFile: fileName) {
imageView.image = image
}
这篇关于在iOS中使用Swift删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文