0

我有一个 UICollectionView,它显示的单元格与通过的图像数量一样多。

我想这样做,以便当您点击图像时,它将该图像复制到剪贴板。

任何帮助将不胜感激。

-(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];

}
4

1 回答 1

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,它是发送视图,您可以直接从中提取图像并复制到粘贴板。

于 2013-11-14T21:02:07.237 回答