Core data SwiftUI 2.0:使用新的.fileImporter将图像保存到核心数据
Xcode版本12.2 beta 4(12B5044c) iOS 14.2 目标:使用新的.fileImporter修改器加载和保存图像 我所做的:Core data SwiftUI 2.0:使用新的.fileImporter将图像保存到核心数据,core-data,swiftui,ios14,xcode12beta6,Core Data,Swiftui,Ios14,Xcode12beta6,Xcode版本12.2 beta 4(12B5044c) iOS 14.2 目标:使用新的.fileImporter修改器加载和保存图像 我所做的: //load images .fileImporter(isPresented: $openFile, allowedContentTypes: [.image]) { (res) in do{ let fileUrl =
//load images
.fileImporter(isPresented: $openFile, allowedContentTypes: [.image]) { (res) in
do{
let fileUrl = try res.get()
print(fileUrl)
//getting filename
self.fileName = fileUrl.lastPathComponent
}
catch{
print ("error reading")
print (error.localizedDescription)
}
}
//save field inputs to Core Data
private func addItem() {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
newItem.itemNumber = self.itemNumber
newItem.name = self.name
// need to save URL as data, but cannot figure out
newItem.coverImage = self.fileName
do {
try viewContext.save()
print("Item saved.")
presentationMode.wrappedValue.dismiss()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
问题:
我只能用这种方法保存字符串(图像名称)
我知道我需要将图像转换为数据,以便能够保存核心数据
有人知道如何将图像转换为数据并保存吗
干杯