【问题标题】:swift core data: save images from cameraswift核心数据:保存相机中的图像
【发布时间】:2016-04-01 13:36:28
【问题描述】:

我使用 Swift,并且正在开发一个带有“相机”按钮的应用程序。如果我单击此按钮,iPhone 相机将打开。但是,当我转到“使用照片”时,该应用程序不起作用。

如何将拍摄的图像保存在一个数组中,以便它们在UITableView 中列出?

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete{
        imageArray.removeAtIndex(indexPath.row)
        self.mytableview.reloadData()
    }
}

【问题讨论】:

    标签: swift core-data camera swift2


    【解决方案1】:

    您正在将图像设置为 imagePicker 实例,这将无法正常工作。相反,您希望:

    1. 将图像序列化为 NSData 对象
    2. 将 NSData 对象存储在数据库中
    3. 从 TableView 的数据源中的数据库中读回每个图像 要获取 NSData 对象,您可以将图像转换为 PNG。你应该在你的 didFinishPickingImage 委托函数中加入这样的东西:

      让 imgData = UIImagePNGRepresentation(图像) item.setValue(imgData, forKey: "image") 上面的代码假设您的数据模型有一个类型为 Data 且名称为 image 的列。然后它将转换后的图像插入数据库。

    现在,在您的 tableViewDelegate 的 cellAtRowForIndexPath 中,从数据库中读取每个图像的数据并将它们放入单元格中,使用如下内容: cell.imageView.image = UIImage(data: imgData)

    但是,我不建议使用这种方法来存储全分辨率照片。 Core Data 设计用于相对较小的值,而不是全分辨率图像。 最好将图像存储在具有标准化命名约定的文件系统(应用程序的文档文件夹)中。

    【讨论】:

      猜你喜欢
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      相关资源
      最近更新 更多