拍照并存储在coredata中

时间:2015-07-06 02:39:20

标签: ios swift core-data uiimageview uiimagepickercontroller

在一个视图控制器中,我拍摄照片并存储在coredata中:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    var image = info[UIImagePickerControllerEditedImage] as? UIImage
    if image == nil{
        image = info[UIImagePickerControllerOriginalImage] as? UIImage
    }
    imageView.image = image
    let newImageDescription = NSEntityDescription.entityForName("Image", inManagedObjectContext: context!) 
    let newImage = Image(entity: newImageDescription!, insertIntoManagedObjectContext: context)
    newImage.image = imageView
    var error = NSError?()
    context?.save(&error)

    dismissViewControllerAnimated(true, completion: nil)
}

另一方面,我在集合视图中显示图像:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
    let store = stores[indexPath.row]

    var imageView = cell.viewWithTag(1) as UIImageView //ERROR HAPPENS HERE

    imageView.image = UIImage(data: store.image as NSData)

但后来我遇到致命错误:在解开Optional值时意外地发现了nil。我知道它可以节省,因为当我拍照并打印stores.count时,我得到了拍摄的照片数量。所以我想将我保存的图像显示在UIImages的集合视图中。我做错了什么?

0 个答案:

没有答案