ios - 保存到相机胶卷后从路径加载特定图像
问题描述
我使用 UIImagePickerController 将图像保存到相机拍摄的相册中。
UIImageWriteToSavedPhotosAlbum(myImage, self, #selector(saveimage(_:didFinishSavingWithError:contextInfo:)), nil)
图像已成功保存到相册,名称为IMAGE_4000.JPG。我想获得直接加载图像的路径,而无需选择它来实现基于 UI 的用户选择。
我尝试执行以下操作,但它只能让我访问应用程序的特定文件夹(如/var/mobile/Containers/Data/Application/3C739746-<...>/Documents/)而不是相册。
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let fullpath = path[0].appending("/IMAGE_4000.JPG")
let url = URL(fileURLWithPath: fullpath)
let imagedata = try? Data(contentsOf: url)
let imagefromstorage = UIImage(data: imagedata!)
所以我的问题是,如何在通过UIImageWriteToSavedPhotosAlbum保存图像名称以及访问它的相册路径后获取图像名称,就像我在上面的代码片段中描述的那样?
解决方案
UIImageWriteToSavedPhotosAlbum
将图像保存到Camera Roll
相册。这是一个不同的容器。
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
上面的语句将在你的 App 容器中搜索文件。
所以两者是不同的东西。
推荐阅读
- c - C整数指针内存地址突然改变
- transformation - 颠簸变换将大数组转换为小数组
- java - 如何使用 java regex 从字符串中排除唯一的子字符串(不是它们的字符)?
- android - NDEF 格式的 Android HCE 并使用 iOS CoreNFC 阅读器应用程序读取它
- regex - 正则表达式“不包含”
- javascript - AngularJS:angular.js:11607 TypeError:无法设置未定义的属性“0”
- sql-server - 从 SQL 查询派生值
- json - 从包含多个值的 json 输出文件中提取最新值
- python - 在 Multiindex 数据框中查找每个索引的第一次出现
- java - 阻塞 EntityManager 操作