在swift 2中加载图像和CoreData

时间:2016-07-24 12:30:39

标签: ios swift core-data

我正在开发一个应用程序,其中我有一个包含四个字段的表单,并且我希望能够使用相册为每个字段提取并选择一张照片,除了它是coreData并显示下一个在我点击提交(GENERATE按钮)之前。我正在使用这个Saving Picked Image to CoreData和github:https://github.com/romainmenke/SimpleCam作为指南,我可以为1张图片保存和加载,但是当我尝试四张图片并将其保存在coreData中时。它确实工作有更好的方法这样做我的主要问题是加载图像时,我运行它有时工作在一张图片上,然后其他图片不加载或它不加载对于任何图片。

enter image description here

这是我使用的代码点击我点击相机图标:

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

这是我通过函数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
        }
    }
}
}

,完整代码位于github:https://github.com/samauto/PicMadLibs/tree/master/PicMadLibs

0 个答案:

没有答案