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 に答える