2013-10-28 22 views
0

我有一個UICollectionView,顯示的圖像數量與傳遞的圖像數量一樣多。將UIImage複製到剪貼板上水龍頭

我想讓它在點擊圖片時將圖片複製到剪貼板。

任何幫助將不勝感激。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
      cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
CollectionViewCell *Cell = [collectionView 
          dequeueReusableCellWithReuseIdentifier:@"ImagesCell" 
          forIndexPath:indexPath]; 

UIImage *image; 
int row = indexPath.row; 

//set image of this cell 
image = [UIImage imageNamed:localImages.imageFiles[row]]; 
Cell.imageCell.image = image; 


//enable touch 
[Cell.imageCell setUserInteractionEnabled:YES]; 
Cell.imageCell.tag = row; 
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)]; 
[tapGesture setNumberOfTapsRequired:1]; 
[Cell.imageCell addGestureRecognizer:tapGesture]; 

return Cell; 
} 


- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender 
{ 

NSInteger string = (sender.view.tag); 
NSLog(@"this is image %i",string); 

UIImage *copiedImage = I don't know what to put here.. 
[[UIPasteboard generalPasteboard] setImage:copiedImage]; 

} 

回答

0

它實際上如果你只是實現UICollectionView - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath的委託方法更簡單,只是這樣做:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    UIImage *copiedImage = cell.imageCell.image; 
    [[UIPasteboard generalPasteboard] setImage:copiedImage]; 
} 

希望這有助於你出去!

編輯:如果你有圖片和文字不同類型的細胞,而你只想要啓用下的圖像複製,你將不得不執行該UITapGestureRecognizer部分,並在方法中調用你只需要做到這一點:

- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender 
{ 

UIImageView *imageView = (UIImageView *)sender.view; 
UIImage *copiedImage = imageView.image; 
[[UIPasteboard generalPasteboard] setImage:copiedImage]; 

} 

由於您的UITapGestureRecognizer附加到UIImageView,它是發送視圖,您可以直接從它提取圖像並複製到粘貼板。