将UIImage数组保存到磁盘

时间:2017-07-04 16:55:35

标签: swift uiimage

我有一个UIImage数组,我正在尝试保存到磁盘(文档目录)。我已将其转换为Data,因此可以将其保存,但我无法找出正确/最佳方式将[Data]保存到磁盘

(arrayData as NSArray).write(to: filename, atomically: false)似乎不对,或者似乎有更好的方法。

到目前为止

代码:

    // Get the new UIImage
    let image4 = chartView.snapShot()

    // Make UIImage into Data to save
    let data = UIImagePNGRepresentation(image4!)

    // Add Data image to Data Array
    arrayData.append(data!)

    // Get Disk/Folder
    let filename = getDocumentsDirectory().appendingPathComponent("images")

    // This is how I'd Write Data image to Disk
    // try? data?.write(to: filename)

    // TODO: Write array Data images to Disk
    (arrayData as NSArray).write(to: filename, atomically: false)

2 个答案:

答案 0 :(得分:1)

您可以在下面将代码存储到本地。

   let fileManager = NSFileManager.defaultManager()
    let paths = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString).stringByAppendingPathComponent("ImageName.png")
    let image = YOUR_IMAGE_TO_STORE.
    let imageData = UIImageJPEGRepresentation(image!, 0.5)
    fileManager.createFileAtPath(paths as String, contents: imageData, attributes: nil)

答案 1 :(得分:1)

要将图像保存到文档目录,可以先创建一个保存图像的功能,如下所示。该函数处理为您创建图像名称并返回它。

    func saveImage(image: UIImage) -> String {

    let imageData = NSData(data: UIImagePNGRepresentation(image)!)
    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

   }

现在既然你想保存一组图像,你可以简单地循环并调用saveImage函数:

   for image in images { 
       saveImage(image: image)
   }

如果您希望自己命名这些图像,可以将第一个功能修改为以下内容:

    func saveImage(image: UIImage, withName name: String) {

 let imageData = NSData(data: UIImagePNGRepresentation(image)!)
 let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,  FileManager.SearchPathDomainMask.userDomainMask, true)
 let docs = paths[0] as NSString
 let name = name
 let fullPath = docs.appendingPathComponent(name)
 _ = imageData.write(toFile: fullPath, atomically: true)
 }

当您尝试将图像保存为UIImagePNGRepresentation时,请注意,这不会为您保存方向。这意味着当您尝试检索图像时,它可能会标题为左侧。您需要重新绘制图片以确保其方向正确,因为这篇文章将解释how to save png correctly。但是,如果您使用UIImageJPEGRepresentation保存图像,则无需担心所有这些,您只需保存图像而无需任何额外的努力。

编辑给予图像阵列

要检索已保存的图像,可以使用以下函数传递图像名称数组,然后返回数组:

   func getImage(imageNames: [String]) -> [UIImage] {

    var savedImages: [UIImage] = [UIImage]()

    for imageName in imageNames {

    if let imagePath = getFilePath(fileName: imageName) {
        savedImage.append(UIImage(contentsOfFile: imagePath))
    }

   }

   return savedImages

   }

依赖于此功能:

    func getFilePath(fileName: String) -> String? {

    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
    var filePath: String?
    let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
   if paths.count > 0 {
   let dirPath = paths[0] as NSString
   filePath = dirPath.appendingPathComponent(fileName)
   }
  else {
  filePath = nil
  }

  return filePath
  }