gpt4 book ai didi

快速核心数据 : save images from camera

转载 作者:可可西里 更新时间:2023-11-01 01:36:47 25 4
gpt4 key购买 nike

我使用 Swift,我正在开发一个带有“相机”按钮的应用程序。如果我点击这个按钮,iPhone 相机就会打开。但是,当我转到“使用照片”时,该应用程序无法运行。

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

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

最佳答案

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

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

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

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

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

关于快速核心数据 : save images from camera,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36357777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com