3

I have simple scenario in my app.

I have 1 edit UIButton to select image from Photo Library and show that selected image in UIImageView.

@IBAction func btnEdit(_ sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
        let picker:UIImagePickerController = UIImagePickerController()
        picker.sourceType = .photoLibrary
        picker.delegate = self
        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        picker.navigationBar.isTranslucent = false
        self.present(picker, animated: true)
    } else {
        print("Photo Library is not available.")
    }

}

UINavigationControllerDelegate & UIImagePickerControllerDelegate

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    if let pickedimage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        self.imageView.image = pickedimage
    } else if let pickedimage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        self.imageView.image = pickedimage
    } else {
        print("Something went wrong while select photo from Library...!")
    }

    dismiss(animated: true, completion: nil)

}

Its working well. But selected image is saving automatically in tmp folder

I don't want to save in Document directory.

enter image description here

How to stop automatically saving images in Document directory?

Jay Patel
  • 2,642
  • 2
  • 18
  • 40

2 Answers2

1

Does the UIImagePickerController.InfoKey contain a value for the imageURL key?
If so, does it map to this location in your tmp directory?

My understanding of the documentation is that UIImagePickerController creates a copy of the image selected for use by your app so you can manipulate it without worrying about affecting the user's image library. That copy is stored in the tmp directory. If you want to clear that copy for some reason you are responsible for deleting the image.

From the Apple Documentation on the iOS app file system (emphasis added):

tmp/

Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when they are no longer needed; however, the system may purge this directory when your app is not running. The contents of this directory are not backed up by iTunes or iCloud.

https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW4

This clean up, assuming you capture the imageURL would be:

let fmTmp = FileManager.default
try! fmTmp.removeItem(at: theImageURL)
Chip Coons
  • 3,201
  • 1
  • 24
  • 16
0

Document directory and Temporary directory are not same. tmp folder will be cleaned when your app is not running. if Necessary you can delete content of tmp folder when the Use of the image is done. How to delete tmp folder content!

  • 1
    No, tmp folder is not cleaned when app is in foreground/not running/killed. All images and tmp folder is there even after restart device. Delete tmp file is only option. But how to stop automatically saving? – Jay Patel Jul 11 '18 at 07:29
  • I don't think there is any option to stop auto saving in tmp folder. As per apple documentation it should be cleaned, maybe it's cleaned in low memory. – S.M.Moinuddin. Shuvo Jul 13 '18 at 11:05