如何从Swift中的文档文件夹中检索保存的UIImageView?
我正在创建这个应用程序,它可以拍摄照片并在UIImageView中显示。当我按下保存按钮时,它会将图像保存到文档目录中。然后我希望能够在我的收藏视图中检索这些图像。我有下面保存图像的代码,但如何在我的收藏视图中检索它?谢谢 第一视图控制器-保存图像如何从Swift中的文档文件夹中检索保存的UIImageView?,swift,uicollectionview,uiimageview,Swift,Uicollectionview,Uiimageview,我正在创建这个应用程序,它可以拍摄照片并在UIImageView中显示。当我按下保存按钮时,它会将图像保存到文档目录中。然后我希望能够在我的收藏视图中检索这些图像。我有下面保存图像的代码,但如何在我的收藏视图中检索它?谢谢 第一视图控制器-保存图像 func saveImage(image: UIImage) -> String { let imageData = NSData(data: image.pngData()!) let paths = NSSearchPathForDire
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 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)
}
}
}
我在这行
self.saveImageToDocumentDirectory(image:self.imageView.image!,imageName:“images”)
上收到一个错误,它说在展开可选值时意外地发现了零。不要强制展开self.imageView.image
使用if let
调用它来检查您的imageview是否有图像…比如if let image=self.imageview.image{self.saveImageToDocumentDirectory(image:image,imageName:images”)}或者{print(“image not found”)}
谢谢,但是由于某种原因它没有保存在文档目录中。保存和检索图像时,它会打印出未找到的图像
。