簡體   English   中英

在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將包含所選單元格中圖像的副本。 希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM