首页 > 解决方案 > 保存到相机胶卷后从路径加载特定图像

问题描述

我使用 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保存图像名称以及访问它的相册路径后获取图像名称,就像我在上面的代码片段中描述的那样?

标签: iosswift

解决方案


UIImageWriteToSavedPhotosAlbum将图像保存到Camera Roll相册。这是一个不同的容器。

let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)

上面的语句将在你的 App 容器中搜索文件。

所以两者是不同的东西。


推荐阅读