Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import CoreData
- class ViewController: UIViewController {
- var canVasView = UIImageView()
- @objc func hhh() {
- let photo = self.canVasView.image
- let data = photo!.pngData()
- if cdHandler.saveObject(pic: data!){
- }
- }
- }
- import UIKit
- import CoreData
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
- var window: UIWindow?
- class cdHandler: NSObject {
- private class func getContext() -> NSManagedObjectContext {
- let appdeleagetzz = UIApplication.shared.delegate as! AppDelegate
- return appdeleagetzz.persistentContainer.viewContext
- }
- class func saveObject(pic: Data) -> Bool {
- let context = getContext()
- let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
- let managedObject = NSManagedObject(entity: entity!, insertInto: context)
- managedObject.setValue(pic, forKey:"pic")
- do {
- try context.save()
- return true
- } catch {
- return false
- }
- }
- class func deletObject(user: User) -> Bool {
- let context = getContext()
- context.delete(user)
- do {
- try context.save()
- return true
- } catch {
- return false
- }
- }
- class func fetchObject() -> [User]? {
- do {
- let context = getContext()
- return try context.fetch(User.fetchRequest())
- } catch {
- return [User]()
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement