我将图像保存在Documents Directory中,并且允许用户编辑图像,因此,如果用户要更改图像,则要删除上一张图像,然后再保存新图像。在同一条路径上。
有我的删除功能:
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数据设置时,只要更改图像就可以看到,我的App数据正在增长。现在我有100mb的数据。
为什么它不能正常工作?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句