将Web内容从safari复制到可信的UIWebView时过滤UIPasteboard

tan*_*boy 7 iphone contenteditable uiwebview uipasteboard ios

我的应用程序是一个丰富的编辑器,它使用UIWebView和HTML5的contenteditable功能.

现在,我的应用程序有一些问题来处理复制网站内容由Safari浏览我的UIWebView当网页内容有任何image.I要像HTML标签替换<img>UIPasteboard我的一些图片.

但是,当我使用以下代码检查粘贴板时,粘贴板中没有任何图像.但从safari复制/粘贴图像到我的应用程序工作正常.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSLog(@"clip board strings = %@", pasteboard.strings);
NSLog(@"clip board urls = %@", pasteboard.URLs);
NSLog(@"clip board images = %@", pasteboard.images);
Run Code Online (Sandbox Code Playgroud)

如果我从safari复制一些图像和文本然后打开我的应用程序,NSLog如下:

2012-04-20 14:53:37.558  clip board strings = (
    "text"
)
2012-04-20 14:53:37.559 [46800:17903] clip board urls = (
)
2012-04-20 14:53:37.559 [46800:17903] clip board images = (null)
Run Code Online (Sandbox Code Playgroud)

我想问一下,如何从UIPasteboard获取图像信息以及如何操作它?

提前致谢.

Abh*_*ngh 1

您可以使用以下方法从粘贴板中提取图像:

UIImage *image = [UIPasteboard generalPasteboard].image; 
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令检查粘贴板是否包含图像文件:

BOOL imgPasteBoard= [[pasteboard pasteboardTypes] containsObject:@"public.png"]; 
Run Code Online (Sandbox Code Playgroud)

然后通过检查布尔值。您也可以尝试使用此代码

[[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListImage]; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];  
if (pasteboard.image!= nil) 
{ 
   [self insertImage:pasteboard.image]; 
   //insert image being a function where you write the code to insert Images to psteboard
} 
Run Code Online (Sandbox Code Playgroud)

希望这对你有帮助..

现在使用以下代码加载 UIImage :-

NSData *imageData = UIImagePNGRepresentation(image);
[webView loadData:imageData MIMEType:imageMIMEType textEncodingName:nil baseURL:nil];
Run Code Online (Sandbox Code Playgroud)