从相册中选择图像时,将图像保存到核心数据

时间:2021-12-15 16:33:47

I have used Eureka to create a form and I have installed the image framework. I have added an image row but I am struggling to save the image to coredata.

我使用Eureka创建了一个表单,我已经安装了图像框架。我添加了一个图像行,但我正在努力将图像保存到coredata。

  • The attribute in coredata is set to binary data
  • coredata中的属性设置为二进制数据

  • Based on how my user form works, I need to declare my image variable globally.
  • 根据我的用户表单的工作方式,我需要全局声明我的图像变量。

I think my problem is connecting the relationship between NSdata and UIImage. The tutorials I find handles this through UIImagePNGRepresentation. But they know the image file name. I don't know this as I select the file from photos.

我认为我的问题是连接NSdata和UIImage之间的关系。我找到的教程通过UIImagePNGRepresentation处理这个问题。但他们知道图像文件名。当我从照片中选择文件时,我不知道这一点。

Global variable = var otsPhoto: NSData? = nil

全局变量= var otsPhoto:NSData? =没有

Form field

   <<< ImageRow() {
            $0.title = "Attachment"
            $0.sourceTypes = [.PhotoLibrary, .SavedPhotosAlbum, .Camera] //1
            $0.value = otsPhoto //2
            $0.clearAction = .yes(style: .destructive) //3
            $0.onChange { [unowned self] row in //4
                self.otsPhoto = row.value
            }

Returns 2 errors

返回2个错误

1) cannot assign value type NSData? to type UIImage?

1)不能赋值类型NSData?输入UIImage?

2) Cannot convert value of type (_) -> to expected argument

2)不能将类型(_) - >的值转换为期望的参数

1 个解决方案

#1


0  

It is not advisable to store Images into Sqlite file. The best approach would be to:

不建议将Images存储到Sqlite文件中。最好的方法是:

  1. Copy the Image from the Photos using UIImagePickerController to the application bundle or download the image from the server to the app bundle.
  2. 使用UIImagePickerController将照片从照片复制到应用程序包,或将图像从服务器下载到应用程序包。

  3. Get the path of the image stored from the application bundle. (you will already know this as you are storing at some location in the app bundle)
  4. 获取从应用程序包中存储的图像的路径。 (当您存储在应用程序包中的某个位置时,您已经知道了这一点)

  5. Store this path as a String in your table
  6. 将此路径存储为表中的String

This way even if the photo is deleted from the Photos, there is a copy of it in your app.

这样即使照片从照片中删除,也会在您的应用中提供相应的照片。

Some resources

https://developer.apple.com/documentation/uikit/uiimagepickercontroller https://developer.apple.com/documentation/foundation/nsfilemanager

#1


0  

It is not advisable to store Images into Sqlite file. The best approach would be to:

不建议将Images存储到Sqlite文件中。最好的方法是:

  1. Copy the Image from the Photos using UIImagePickerController to the application bundle or download the image from the server to the app bundle.
  2. 使用UIImagePickerController将照片从照片复制到应用程序包,或将图像从服务器下载到应用程序包。

  3. Get the path of the image stored from the application bundle. (you will already know this as you are storing at some location in the app bundle)
  4. 获取从应用程序包中存储的图像的路径。 (当您存储在应用程序包中的某个位置时,您已经知道了这一点)

  5. Store this path as a String in your table
  6. 将此路径存储为表中的String

This way even if the photo is deleted from the Photos, there is a copy of it in your app.

这样即使照片从照片中删除,也会在您的应用中提供相应的照片。

Some resources

https://developer.apple.com/documentation/uikit/uiimagepickercontroller https://developer.apple.com/documentation/foundation/nsfilemanager