简体   繁体   English

在UICollectionViewCell中获取图像

[英]Getting image inside UICollectionViewCell

I have a collection view. 我有一个收藏夹视图。 Every cell of this collection view has a UIIMageView marked with tag 100. 此集合视图的每个单元格都有一个带有标签100的UIIMageView。

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. 我想在该单元格的imageView中复制该图像。

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. touchedCellimageView不为零。

Why is that and how do I obtain that image? 为什么会这样,我如何获得该图像?

Follow these-- First create an instance variable 遵循这些-首先创建一个实例变量

@implementation MyClass {
 UIImage *newImage
}

Then Implement this delegate method of collectionview 然后实现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. 因此,您的实例变量newImage将包含所选单元格中图像的副本。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM