我现在遇到了这个问题。我不知道如何保存在写板下方并检索它。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let AppDelegate = (UIApplication.shared.delegate as? AppDelegate)
{
let context = AppDelegate.persistentContainer.viewContext
do{
let results = try context.fetch(VisitorMO.fetchRequest())
let signintime = results[lastnumber!] as? VisitorMO
// signintime?.signinimage = imageView.image
AppDelegate.saveContext()
}catch{
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
答案 0 :(得分:0)
import UIKit
import CoreData
class ViewController: UIViewController {
var logscreen: [NSManagedObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func saveImage(){
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
do{
let entity = NSEntityDescription.entity(forEntityName: "CoreImage", in: managedContext)!
let data = NSManagedObject(entity: entity, insertInto: managedContext)
data.setValue(imageview.image, forKeyPath: "image")
try managedContext.save()
logscreen.append(data)
} catch let error as NSError {
// failure
print(error)
}
}
}
尝试使用此代码进行保存。也将图像转换为nsdata而不是imageview.image