使用UIPasteboard从UIWebView获取图像

时间:2015-08-28 09:33:29

标签: ios uiwebview uipasteboard

我试图使用UIWebViewUIPasteboard复制图像。

[_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 个答案:

答案 0 :(得分:0)

我找到了解决方案:

1)在网页视图中添加长按手势

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”是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让用户保存选择。