[英]delete a file using swift in ios
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
So simply, I get my filename using that function.And it returns correctly for now, how can I delete this file using Swift 3?很简单,我使用该函数获取我的文件名。它现在可以正确返回,如何使用 Swift 3 删除这个文件? And How can I read the image back like
UIImageJPEGRepresentation
?以及如何像
UIImageJPEGRepresentation
一样读取图像?
let image_data = UIImageJPEGRepresentation(self.commons.correctlyOrientedImage(image: self.imageSelected.image!),0.5)
I did following and its not working我做了以下工作,但它不起作用
let filename = getDocumentsDirectory().appendingPathComponent("l.png") let
fileManager = FileManager.default var filePath = filename // Check if file exists
fileManager = FileManager.default var filePath = filename // 检查文件是否存在
if fileManager.fileExists(atPath: filePath.absoluteString) { // Delete file try fileManager.removeItem(atPath: filePath.absoluteString) } else { print("File does not exist") }
I save the file like this我像这样保存文件
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)")
}
And here's code for setting it to imageView:这是将其设置为 imageView 的代码:
if let image = UIImage(contentsOfFile: fileName) {
imageView.image = image
}
Try this.尝试这个。 Hope it will work.To delete a file
希望它会工作。删除文件
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)")
}
Source : http://swiftdeveloperblog.com/code-examples/delete-file-example-in-swift/来源: http ://swiftdeveloperblog.com/code-examples/delete-file-example-in-swift/
For image loading :对于图像加载:
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)!
you could put your remove code in DispatchQeue.main.async
like this:您可以将删除代码放在
DispatchQeue.main.async
中,如下所示:
if fileManager.fileExists(atPath: filePath.absoluteString) {
// Delete file
DispatchQueue.main.async {
try fileManager.removeItem(atPath: filePath.absoluteString)
}
} else {
print("File does not exist")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.