在 UICollectionViewCell 中获取图像

Getting image inside UICollectionViewCell

我有一个集合视图。此集合视图的每个单元格都有一个标有标签 100 的 UIIMageView。

在某一时刻,显示单元格。用户选择一个单元格。我想在该单元格的 imageView 中制作该图像的副本。

这是代码:

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

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

UIImage *image = imageView.image;

问题是此时图像为零。 touchedCellimageView 不为零。

为什么会这样,我如何获得该图像?

关注这些-- 首先创建一个实例变量

@implementation MyClass {
 UIImage *newImage
}

然后 实现collectionview

的这个委托方法
-(void)collectionView:(UICollectionView *)collectionView 
   didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

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

因此您的实例变量 newImage 将包含您所选单元格中的图像副本。 希望这有帮助