gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:00:52 25 4
gpt4 key购买 nike

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

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com