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.
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