如何使用核心数据保存下面的写板(用于签名)

时间:2017-07-07 09:32:24

标签: swift core-data swift3

我现在遇到了这个问题。我不知道如何保存在写板下方并检索它。

enter image description here

 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)")
        }
    }

}

1 个答案:

答案 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