当从Safari复制Web内容到contenteditable UIWebView时过滤UIPasteboard

7
我的应用是一个富文本编辑器,使用了UIWebView和HTML5的contenteditable特性。
现在,我的应用在将带有图片的网页内容从Safari复制到UIWebView时遇到了一些问题。我希望能够将剪贴板中的图片HTML标签替换为我自己的某些图片。
然而,当我使用以下代码检查剪贴板时,剪贴板中没有任何图像。但是从Safari复制/粘贴图像到我的应用程序工作正常。
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSLog(@"clip board strings = %@", pasteboard.strings);
NSLog(@"clip board urls = %@", pasteboard.URLs);
NSLog(@"clip board images = %@", pasteboard.images);

如果我从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)

我想问一下,如何从UIPasteboard获取图像信息并进行操作?

提前致谢。

1个回答

1
您可以使用以下方法从剪贴板中提取图像:
UIImage *image = [UIPasteboard generalPasteboard].image; 

您可以使用以下代码检查剪贴板是否包含图像文件:
BOOL imgPasteBoard= [[pasteboard pasteboardTypes] containsObject:@"public.png"]; 

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

[[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
} 

希望这能对您有所帮助。
现在使用以下代码加载UIImage:-
NSData *imageData = UIImagePNGRepresentation(image);
[webView loadData:imageData MIMEType:imageMIMEType textEncodingName:nil baseURL:nil];

这似乎不起作用,当我复制带有文本的图像时,它只是无法获取图像。 - zolibra
如何将这个UIImage加载到UIWebview中? - zolibra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接