2015-08-28 53 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讓用戶保存選擇。