ios - 如何防止在 Swift 中从 UIImagePickerController 自动保存图像?

标签 ios swift uiimageview swift4 uiimagepickercontroller

我的应用程序中有一个简单的场景。

我有 1 个编辑 UIButton 以从照片库中选择图像并在 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)

}

它运行良好。但是选择的图像自动保存在 tmp 文件夹中

我不想保存在文档目录中。

enter image description here

如何停止在Document目录下自动保存图片?

最佳答案

UIImagePickerController.InfoKey 是否包含 imageURL 键的值?
如果是这样,它是否映射到您的 tmp 目录中的这个位置?

我对文档的理解是,UIImagePickerController 会为您的应用创建所选图像的副本,这样您就可以对其进行操作,而不必担心影响用户的图像库。该副本存储在 tmp 目录中。如果您出于某种原因想要清除该副本,您有责任删除该图像。

来自 iOS 应用程序文件系统上的 Apple 文档(添加了重点):

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

这个清理,假设你捕获 imageURL 将是:

let fmTmp = FileManager.default
try! fmTmp.removeItem(at: theImageURL)

关于ios - 如何防止在 Swift 中从 UIImagePickerController 自动保存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265217/

相关文章:

ios - React-Native 嵌套数组

ios - UITableView 单元格未在 Swift 中更新

ios - 如何更改 Swift 4 中基于发送者的操作函数中使用的属性

cocoa-touch - 如何使用触摸移动 UIImageView

ios - 尝试确保方法检查 Swift 中的字母和连字符

ios - AVAudioSession 操纵声音输出

ios - 为 Xcode 6 iPhone 模拟器移除 iOS 8 UITableView 上的 SeparatorInset

ios - 检查/验证文本字段只能是 1 - 80

ios - 点击 imageView 查看全屏图像

objective-c - 将 uiimage 拖放到另一个 uiimageview 中