I'm saving Images in Documents Directory and I allow user to edit Images, so if user want to change Image, I'm deleting previous image and then I'm saving new one. At the same path.
There is my deleting function:
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)")
}
}
And there is function when I'm trying to overwrite image:
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
}
When I'm changing image, print("Confirmed")
is on Console.
And I've thought it works fine, because User can see new image and this image is shown for user. But When I go to iPhone Data settings, I can see whenever I change image, my App Data is growing. Now I have 100mb of data.
Why It doesn't work properly?