2015-08-28 56 views
2

我试图使用UIPasteboardUIWebView中获取复制图像。使用UIPasteboard从UIWebView中获取图像

[_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,那其实是富文本。有人可以帮忙吗?

回答

0

我已经找到了解决办法:

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”是存储数据图像的NSData类型的类变量,如果存在的话

3)检查fo R数据存在使用获得的文本和图像的方法中选择后

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