0

I am working on an app where I have a form with four fields and and I want to be able to use the photo album to pull up and pick a photo for each of the field, save it is coreData and display it next to it before I click on the submit (GENERATE button). I was using this Saving Picked Image to CoreData and github: https://github.com/romainmenke/SimpleCam as a guide which I am able to do for 1 picture save and load but when I try it for four pictures and save it in coreData. It does work is there a better way of doing this my main issue is with loading the images when I run it it sometime work on one of the picture and then the other pictures don't load or it doesn't load for any of the pictures.

enter image description here

Here is my code that I was using click I click on the camera icon:

extension PML_FormController {

@IBAction func nounCapture(sender: AnyObject) {
    imagePickerCheck()
    entityWord = "NounPhoto"
    self.loadImagesPreview()
}
// END OF FUNC: nounCapture

@IBAction func verbCapture(sender: AnyObject) {
    imagePickerCheck()
    loadImagesPreview()
    entityWord = "VerbPhoto"
}
// END OF FUNC: verbCapture

@IBAction func adverbCapture(sender: AnyObject) {
    imagePickerCheck()
    loadImagesPreview()
    entityWord = "AdverbPhoto"
}
// END OF FUNC: adverbCapture

@IBAction func adjectiveCapture(sender: AnyObject) {
    imagePickerCheck()
    loadImagesPreview()
    entityWord = "AdjectivePhoto"
}
// END OF FUNC: adjectiveCapture

func imagePickerCheck() {
    // unwrap the imagePicker
    guard let imagePicker = imagePicker else {
        cantOpenPicker(withSource: sourceType)
        return
    }

    // present the imagePicker
    presentViewController(imagePicker, animated: true, completion: nil)
}
// END OF FUNC: imagePickerCheck

func loadImagesPreview() {
    // loadImage function with a completion block
    loadImages { (images) -> Void in
        if (self.entityWord == "NounPhoto") {
            if let thumbnailData = images?.last?.thumbnailNoun?.imageData {
                let image = UIImage(data: thumbnailData)
                self.nounImgPreview.image = image
            }

        } else if (self.entityWord == "VerbPhoto") {
            if let thumbnailData = images?.last?.thumbnailVerb?.imageData {
                let image = UIImage(data: thumbnailData)
                self.verbImgPreview.image = image
            }

        } else if (self.entityWord == "AdverbPhoto") {
            if let thumbnailData = images?.last?.thumbnailAdverb?.imageData {
                let image = UIImage(data: thumbnailData)
                self.adverbImgPreview.image = image
            }

        } else {
            if let thumbnailData = images?.last?.thumbnailAdjective?.imageData {
                let image = UIImage(data: thumbnailData)
                self.adjectiveImgPreview.image = image
            }
        }
    }
}
//END OF FUNC: loadImagesPreview

}
//END OF EXTENSION: cameraButCapture

And this is the code when the function I load the images via function loadImagesPreview()

/**
 Load all images saved by the App

 - parameter fetched: Completion Block for the background fetch.
 */
func loadImages(fetched:(images:[FullRes]?) -> Void) {

    startActivity()

    Run.async(coreDataQueue) {

        guard let moc = self.managedContext else {
            return
        }

        let fetchRequest = NSFetchRequest(entityName: "FullRes")

        do {
            let results = try moc.executeFetchRequest(fetchRequest)
            let imageData = results as? [FullRes]

            self.stopActivity()

            Run.main {
                fetched(images: imageData)
                print ("IMAGEDATA", imageData)
            }
        } catch {

            self.stopActivity()

            Run.main {
                self.noImagesFound()
            }
            return
        }
    }
}
}

and the full code is at github: https://github.com/samauto/PicMadLibs/tree/master/PicMadLibs

Community
  • 1
  • 1
samauto
  • 137
  • 2
  • 15
  • and where is your code? I don't think it has to do with your core data. I think the issue is with your `cellForRowAtIndexPath` – jo3birdtalk Jul 24 '16 at 12:48
  • I update with code. Also, I am not using cellFor RowAtIndexPath on the form since it is not a tableview just a four images on the form that I would like it to change. – samauto Jul 25 '16 at 12:30
  • I would appreciate if you did not copy paste my code without including the copyright. The code I posted in my answer still belongs to me, as does all the code in the github repo. There is no license attached to the answer or the repo that allows using it for anything other than learning purposes. – R Menke Jul 27 '16 at 18:21
  • I apologize just a newbie to swift and I was just working on a school project - I will add the copyright to the code --not sure what is the proper procedure for the copyright can I just make a blanket statement that parts of the code are Created by Romain Menke on 03/11/15. Copyright © 2015 Romain Menke. All rights reserved. Since I had modified it to try to fit it to what I am right to do?? – samauto Jul 28 '16 at 04:20
  • If you reference my name and a link to the github repo I am ok with you using it for a school project. There is nothing wrong with using pieces of code from around the internet. Just give credit where credit is due. Have fun! – R Menke Jul 28 '16 at 07:09

0 Answers0