将UIImage复制到剪贴板上

时间:2013-10-28 13:55:52

标签: uiimage uicollectionviewcell sender pasteboard

我有一个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];

}

1 个答案:

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