无法拍照并将其保留在核心数据中

时间:2015-05-15 22:39:25

标签: ios swift core-data uiimagepickercontroller

有人可以帮助UIImagePickerController功能和保存方法吗?我已经能够拍摄照片,甚至可以将它显示在我的UIImageView中,但是还没有能够将它保存在那里,因为我正在处理的文本已被保存。

import UIKit
import CoreData

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    var newKid: Kid? = nil
    var pickerController = UIImagePickerController()

    // MARK: - IBOutlet

    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var ageText: UITextField!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if newKid != nil {
            nameText.text = newKid?.name
            ageText.text = newKid?.age
         // imageView.image = newKid?.image
        } 
    }

    // MARK: - IBAction

    @IBAction func cancelTapped(sender: AnyObject) {
        dismissVC()
    }

    @IBAction func saveTapped(sender: AnyObject) {
        if newKid != nil {
            editEntry()
        } else {
            newEntry()
        }
        dismissVC()
    }

    @IBAction func takePhoto(sender: AnyObject) {
        pickerController.delegate = self
        pickerController.sourceType = .Camera
        self.presentViewController(pickerController, animated: true, completion: nil)
    }

    // MARK: - Helper Functions

    func newEntry() {

        var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

        let entity = NSEntityDescription.entityForName("Kid", inManagedObjectContext: context!)

        let newKid = Kid(entity: entity!, insertIntoManagedObjectContext: context)

        newKid.name = nameText.text
        newKid.age = ageText.text
     // newKid.image = imageView.image
     // context?.save(nil)
        saveEntry()
        println(newKid)
        println("Object saved.")
        dismissVC()
    }

    func editEntry() {
        newKid?.name = nameText.text
        newKid?.age = ageText.text
        newKid?.image = imageView.image
    }

    func saveEntry() {
        var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        context?.save(nil)
    }

    func dismissVC() {
        navigationController?.popToRootViewControllerAnimated(true)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        let mediaType = info[UIImagePickerControllerMediaType] as! String
        self.dismissViewControllerAnimated(true, completion: nil)
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image
    }

    @IBAction func useCamera(sender: AnyObject) {
        if UIImagePickerController.isSourceTypeAvailable(
            UIImagePickerControllerSourceType.Camera) {

                let imagePicker = UIImagePickerController()

                imagePicker.delegate = self
                imagePicker.sourceType = .Camera
                imagePicker.allowsEditing = false

                self.presentViewController(imagePicker, animated: true,
                    completion: nil)
        }
    }
}

0 个答案:

没有答案