使用 UIPasteboard 从 UIWebView 获取图像

Get image from UIWebView using UIPasteboard

我正在尝试使用 UIPasteboardUIWebView 获取复制图像。

[_myWebView copy:[UIApplication sharedApplication]]; //Copy selection to general pasteboard

NSArray* types = [NSArray arrayWithArray:[UIPasteboard generalPasteboard].pasteboardTypes];
NSLog(@"Type %@", [types objectAtIndex:0]);

if([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString]){

    NSLog(@"text selected %@",[UIPasteboard generalPasteboard].string);

}else if([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListImage]){

    NSLog(@"image selected");

}else if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListURL]){

    NSLog(@"url selected");
}

如果我复制文本没问题,但如果我复制图片,我会得到:

WebController.m:445 > Type com.apple.rtfd
WebController.m:458 > text selected {\rtf1\ansi\ansicpg1252
{\fonttbl\f0\fnil\fcharset0 .SFUIText-Regular;}
{\colortbl;\red255\green255\blue255;\red45\green45\blue45;}
\deftab720
\pard\pardeftab720\qc\partightenfactor0
\f0\fs32 \cf2 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 \
}

我尝试了很多来自不同网站的图片,但我总是得到 rtfd 类型,这实际上是富文本。有人可以帮忙吗?

我找到了解决方案:

1) 向您的 Web 视图添加长按手势

UILongPressGestureRecognizer* longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:nil];
    longTap.delegate = self;
    [_myWebView addGestureRecognizer:longTap];

2)实现委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"TAPPED");

    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {

        CGPoint touchPoint = [touch locationInView:self.view];

        NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
        NSString *urlToSave = [_myWebView stringByEvaluatingJavaScriptFromString:imgURL];
        NSURL * imageURL = [NSURL URLWithString:urlToSave];
        NSLog(@"image URL :%@",urlToSave);

        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage * image = [UIImage imageWithData:imageData];
        NSLog(@"Image:%.00fx%.00f",image.size.height,image.size.width);

        if (image != nil) {
            NSLog(@"image exist 1");
            _selectedImageDataAtPoint = imageData;

        }

    }
    return YES;
}

其中“_selectedImageDataAtPoint”是一个 class NSData 类型的变量,用于存储数据图像(如果存在)

3) 在用于获取文本和图像的方法中进行选择后检查数据是否存在

[_myWebView copy:[UIApplication sharedApplication]]; //Copy selection to general pasteboard

    NSArray* types = [NSArray arrayWithArray:[UIPasteboard generalPasteboard].pasteboardTypes];
    NSLog(@"Type %@", [types objectAtIndex:0]);

    if ([[types objectAtIndex:0] isEqualToString:@"com.apple.rtfd"]) {

        if ([UIImage imageWithData:_selectedImageDataAtPoint] != nil) {

            NSLog(@"image exist 2");
            _manualRecipe.imageData = _selectedImageDataAtPoint;
            [self openActivityWithImage];

        }

    }else if([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString]){

        NSLog(@"text selected: %@",[UIPasteboard generalPasteboard].string);
        NSString* text = [UIPasteboard generalPasteboard].string;
        [self openActivityWithText:text];

    }

要点是用户要长按才能从网络中选择文本或图像,因此我添加了另一个 UILongPressGestureRecognizer 来获取图像。之后,我展示我的自定义 UIMenuItem 让用户保存选择。