如何从 Swift 中的文档文件夹中检索保存的 UIImageViews?
How to retrieve saved UIImageViews from documents folder in Swift?
我正在创建此应用程序,它可以拍照并将其显示在 UIImageView 中。当我按下保存按钮时,它应该将图像保存到文档目录中。然后我希望能够在我的 collection 视图中检索这些图像。我有下面的代码可以保存图像,但如何在我的 collection 视图中检索它?谢谢!
第一个视图控制器 - 保存图像
func saveImage(image: UIImage) -> String {
let imageData = NSData(data: image.pngData()!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let uuid = NSUUID().uuidString + ".png"
let fullPath = docs.appendingPathComponent(uuid)
_ = imageData.write(toFile: fullPath, atomically: true)
return uuid
}
@IBAction func didPressSaveButton(_ sender: Any) {
for _ in self.images {
_ = self.saveImage(image: self.imageView.image!)
}
}
第二视图控制器 - 检索图像
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as!
MyPicturesCollectionViewCell
return cell
}
你可以这样取回
func loadImageFromDocumentDirectory(nameOfImage : String) -> UIImage? {
let documentDirectory = FileManager.SearchPathDirectory.documentDirectory
let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)
if let dirPath = paths.first{
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameOfImage)
if let image = UIImage(contentsOfFile: imageURL.path) {
return image
} else {
print("image not found")
return nil
}
}
return nil
}
并用于储蓄
func saveImageToDocumentDirectory(image: UIImage , imageName: String) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = imageName // name of the image to be saved
let fileURL = documentsDirectory.appendingPathComponent(fileName)
if let data = image.jpegData(compressionQuality: 1),
!FileManager.default.fileExists(atPath: fileURL.path){
do {
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}
我正在创建此应用程序,它可以拍照并将其显示在 UIImageView 中。当我按下保存按钮时,它应该将图像保存到文档目录中。然后我希望能够在我的 collection 视图中检索这些图像。我有下面的代码可以保存图像,但如何在我的 collection 视图中检索它?谢谢!
第一个视图控制器 - 保存图像
func saveImage(image: UIImage) -> String {
let imageData = NSData(data: image.pngData()!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let uuid = NSUUID().uuidString + ".png"
let fullPath = docs.appendingPathComponent(uuid)
_ = imageData.write(toFile: fullPath, atomically: true)
return uuid
}
@IBAction func didPressSaveButton(_ sender: Any) {
for _ in self.images {
_ = self.saveImage(image: self.imageView.image!)
}
}
第二视图控制器 - 检索图像
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as!
MyPicturesCollectionViewCell
return cell
}
你可以这样取回
func loadImageFromDocumentDirectory(nameOfImage : String) -> UIImage? {
let documentDirectory = FileManager.SearchPathDirectory.documentDirectory
let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)
if let dirPath = paths.first{
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameOfImage)
if let image = UIImage(contentsOfFile: imageURL.path) {
return image
} else {
print("image not found")
return nil
}
}
return nil
}
并用于储蓄
func saveImageToDocumentDirectory(image: UIImage , imageName: String) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = imageName // name of the image to be saved
let fileURL = documentsDirectory.appendingPathComponent(fileName)
if let data = image.jpegData(compressionQuality: 1),
!FileManager.default.fileExists(atPath: fileURL.path){
do {
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}