【发布时间】:2023-04-03 11:06:01
【问题描述】:
我将图像保存在文档目录中,并且我允许用户编辑图像,因此如果用户想要更改图像,我将删除以前的图像,然后我将保存新图像。在同一条路径上。
还有我的删除功能:
func deleteItemInDirectory(imageName: String) {
let fileManager = FileManager.default
let imagePath = (self.getDirectoryPath() as NSString).appendingPathComponent(imageName)
do {
if fileManager.fileExists(atPath: imagePath) {
try fileManager.removeItem(atPath: imagePath)
print("Confirmed")
}
else {
print("nothing happened")
}
}
catch let error as NSError {
print("An error took place: \(error)")
}
}
当我试图覆盖图像时有功能:
func saveItem() {
if self.nick != ""{
self.ShowingEmptyFieldsAlert = false
let imageName = self.user.imageName!
self.user.name = self.nick
self.user.birthday = self.birthday
if self.pickedImage != nil {
ImageToDirectory().deleteItemInDirectory(imageName: imageName)
ImageToDirectory().saveImageToDocumentDirectory(image: self.pickedImage!, filename: imageName)
}
try? self.moc.save()
self.presentationMode.wrappedValue.dismiss()
}
else {
self.ShowingEmptyFieldsAlert = true
}
当我更改图像时,print("Confirmed") 在控制台上。
而且我认为它可以正常工作,因为用户可以看到新图像并且该图像是为用户显示的。但是当我进入 iPhone 数据设置时,我可以看到每当我更改图像时,我的应用程序数据都在增长。现在我有 100mb 的数据。
为什么它不能正常工作?
【问题讨论】:
标签: ios swift directory document delete-operator