Duck
Duck

Reputation: 36013

Getting image inside UICollectionViewCell

I have a collection view. Every cell of this collection view has a UIIMageView marked with tag 100.

At one point, the cells are displayed. The user selects a cell. I want to make a copy of that image inside the imageView of that cell.

This is the code:

UICollectionViewCell *touchedCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

UIImageView *imageView = (UIImageView *)[touchedCell viewWithTag:100];

UIImage *image = imageView.image;

The problem is that at this point, image is nil. touchedCell and imageView are not nil.

Why is that and how do I obtain that image?

Upvotes: 0

Views: 568

Answers (1)

Saheb Roy
Saheb Roy

Reputation: 5967

Follow these-- First create an instance variable

@implementation MyClass {
 UIImage *newImage
}

Then Implement this delegate method of collectionview

-(void)collectionView:(UICollectionView *)collectionView 
   didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

CustomCollectionViewCell *datasetCell = 
  (CustomCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
 newImage = [UIImage imageWithCGImage:cell.customImageVIew.oldImage.CGImage];
}

So your instance variable newImage will contain a copy of the image from the cell you selected. Hope this helps

Upvotes: 1

Related Questions