Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import UIKit
  2. import CoreData
  3.  
  4. class ViewController: UIViewController {
  5. var canVasView = UIImageView()
  6. @objc func hhh() {
  7. let photo = self.canVasView.image
  8. let data = photo!.pngData()
  9.  
  10. if cdHandler.saveObject(pic: data!){
  11. }
  12. }
  13. }
  14.  
  15. import UIKit
  16. import CoreData
  17.  
  18. @UIApplicationMain
  19. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  20.  
  21. var window: UIWindow?
  22.  
  23.  
  24.  
  25. class cdHandler: NSObject {
  26.  
  27. private class func getContext() -> NSManagedObjectContext {
  28. let appdeleagetzz = UIApplication.shared.delegate as! AppDelegate
  29. return appdeleagetzz.persistentContainer.viewContext
  30. }
  31. class func saveObject(pic: Data) -> Bool {
  32. let context = getContext()
  33. let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
  34. let managedObject = NSManagedObject(entity: entity!, insertInto: context)
  35. managedObject.setValue(pic, forKey:"pic")
  36. do {
  37. try context.save()
  38. return true
  39.  
  40. } catch {
  41. return false
  42.  
  43. }
  44. }
  45. class func deletObject(user: User) -> Bool {
  46. let context = getContext()
  47. context.delete(user)
  48.  
  49. do {
  50. try context.save()
  51. return true
  52. } catch {
  53. return false
  54.  
  55. }
  56. }
  57.  
  58. class func fetchObject() -> [User]? {
  59.  
  60. do {
  61. let context = getContext()
  62. return try context.fetch(User.fetchRequest())
  63. } catch {
  64. return [User]()
  65. }
  66. }
  67.  
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement