【问题标题】:How to retrieve saved UIImageViews from documents folder in Swift?如何从 Swift 中的文档文件夹中检索保存的 UIImageViews?
【发布时间】:2020-06-20 15:57:47
【问题描述】:

我正在创建这个应用程序,它会拍照并将其显示在 UIImageView 中。当我按下保存按钮时,它应该将图像保存到文档目录。然后我希望能够在我的收藏视图中检索这些图像。我有下面的代码保存图像,但我将如何在我的收藏视图中检索它?谢谢!

第一个视图控制器 - 保存图像

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
}

【问题讨论】:

    标签: swift uicollectionview uiimageview


    【解决方案1】:

    你可以这样检索它

    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") 它说 Unexpectedly found nil while unwrapping an Optional value
    • 不要强制解开self.imageView.image!
    • if let调用它来检查你的imageview是否有图像...
    • 点赞if let image = self.imageView.image {self.saveImageToDocumentDirectory(image: image, imageName: "images") } else {print("image not found")}
    • 谢谢你,但由于某种原因它没有保存在文档目录中。它在保存和检索图像时打印出image not found
    猜你喜欢
    • 2011-12-10
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2021-12-31
    • 2021-10-19
    相关资源
    最近更新 更多