0

I have an array with all the images I got from the camera. They are saved in my Core Data. If a user takes a new photo, the photo will be saved in the core data and then I replace the normal array data with the Core Data images. So if I go back to the collection view, only the first and the last image is shown. When I print the images[indexpath.item] out, I got the images. But they aren't shown in the cells. I have a custom class for the imageView in the cell and the imageView has constraints, so the image can't be out of the visible area I think. I look forward to solve this problem with your help. Thank you.

Here is the code from camera catch :

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {                    
    photoImage.image = image
    
    print(images)
    addPhotoLabel.removeFromSuperview()
    if isEdit == false {            
        let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let entinity = NSEntityDescription.entityForName("Image", inManagedObjectContext: context)
        
        let newImage = Image(entity: entinity!, insertIntoManagedObjectContext: context)
        newImage.image = UIImageJPEGRepresentation(image, 1)
        do {
            try context.save()
        } catch let error as NSError {
            print(error)
        }
        images.removeAll()
        let request = NSFetchRequest()
        request.entity = entinity
        let fetched = try? context.executeFetchRequest(request)
        if fetched != nil {
            let imagesS = fetched!.map { UIImage(data: ($0 as! Image).image!)}
            
            images.appendContentsOf(imagesS)
            
        }        
    }
    self.dismissViewControllerAnimated(true, completion: nil);
}

And the code where I create the cell:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1
   
    let cell : FlickrPhotoCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FlickrPhotoCell    

    if searchBarActive == true 
    {
        cell.nameLabel.text = researchResults[indexPath.item]
        cell.imageView.image = sFunc_imageFixOrientation(resultImages[indexPath.item]) 
    }
    else
    {        
    cell.nameLabel.text = names[indexPath.item]
        
    cell.imageView.image = images[indexPath.item]
        print(images[indexPath.item])                
    }

    cell.nameLabel.hidden = false

    cell.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.width/2  , UIScreen.mainScreen().bounds.width/2)

    cell.imageView.frame = cell.frame
        
    cell.backgroundColor = UIColor.clearColor()
    cell.layer.borderColor = themeColor.CGColor
    cell.layer.borderWidth = 4
    cell.layer.cornerRadius = CGFloat(10.0)
    cell.placeHolderName.frame.size = CGSize(width: cell.frame.width, height: cell.frame.height/4)
 
    return cell
}

Ask me if you need more code. Thank you

shim
  • 9,289
  • 12
  • 69
  • 108
Paul Heinemeyer
  • 217
  • 1
  • 2
  • 18
  • 1
    Possible duplicate of [Images arent shown in my collectionviewcells. iOS(swift)](http://stackoverflow.com/questions/38792898/images-arent-shown-in-my-collectionviewcells-iosswift) – ff10 Aug 05 '16 at 17:33
  • 1
    Here's what to do when no one answers your question. http://stackoverflow.com/help/no-one-answers – ff10 Aug 05 '16 at 18:27

1 Answers1

0

This answer might be of some help: https://stackoverflow.com/a/14438875/2678994

Also, imo you may want to (in your Storyboard or programmatically) setup a UIImageView within the UICollectionViewCell, then update that image view's .image value.

Community
  • 1
  • 1
DTHENG
  • 188
  • 1
  • 7