1

Can someone please help with an UIImagePickerController function and a way to save? I have been able to take a photo and even have it show up in my UIImageView, but have yet to be able to keep it there, where as the text I'm working with has been saved.

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)
        }
    }
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • Have a look at this answer that shows how to store and retrieve a UIImage from CoreData: http://stackoverflow.com/a/3844130/78496 – chedabob May 15 '15 at 22:51
  • This is a bit broad. What have you done so far to attempt to save the image? – rmaddy May 15 '15 at 22:51
  • Thank you chedabob, but I don't understand obj-c...a bit of a newbie. rmaddy, image is stored as an NSManagedObject in my model class, but I can't seem to convert the image taken with image picker to an NSData object. It's all really confusing. I've commented the image section in the function newEntry because I was getting the NSDATA error. And I thought the way you save it once is just context?.save(nil).... – user1168132 May 15 '15 at 22:54
  • As you can see, I'm still searching for a solution to this problem. I don't think the question is too broad. I understand how to take the photo, get it to show up in the UIImageview, I just can't get it to persist in Core Data. I've read I need to convert it to NSData using a dictionary, but I am unsure as to how to complete this task. Any help is appreciated! – user1168132 Jun 03 '15 at 16:50
  • Any and all info is appreciated!!!!!! – user1168132 Jun 29 '15 at 05:03

0 Answers0