ios - 在 UICollectionViewCell 中获取图像

标签 ios iphone uiimageview uiimage uicollectionview

我有一个 Collection View 。此 Collection View 的每个单元格都有一个标有标签 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的这个委托(delegate)方法

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

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

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

关于ios - 在 UICollectionViewCell 中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32347616/

相关文章:

iphone - UIViewController IBOutlets 为零

iOS swift : LaunchScreen doesn't show image

ios - 基本的 iOS 代码行解释

iphone - 在 iPhone 中访问/dev/tty.iap 被拒绝

objective-c - 使用 Core Data 时提高 UI 性能

ios - swift 3 : How to get language code from a country code?

ios - 如何快速从文件路径中获取文件名

ios - CocoaPods:添加到项目的新目标找不到 CocoaPods header

iphone - PhoneGap 2.1 : after upgrade (2. 0 到 2.1) onDeviceReady 未被远程调用

ios - 为什么我放置在 ScrollView 中的 ImageView 具有扭曲的高度?