将本地图像加载到 UIWebView 中
我想截取屏幕截图,然后将其本地路径发送到 webview,这样它就会显示它。我知道 webview 可以从 NSBundle 获取图像,但此屏幕截图是动态创建的 - 我可以通过代码向其中添加一些内容吗?
我正在尝试这样的操作:
UIGraphicsBeginImageContext( webView.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *file = @"myfile.png";
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *path = [directory stringByAppendingPathComponent: file];
[UIImagePNGRepresentation(viewImage) writeToFile:path atomically:YES];
NSString *function = [NSString stringWithFormat:@"loadImagePlease(%@);",path];
[ ad stringByEvaluatingJavaScriptFromString:function];
但是 loadImageFunction 无法使用 file:/// 或 file:// 前缀访问它。解决办法是什么? 感谢您的任何帮助。
编辑:在代码中添加了缺失的行
解决方案:我使用了西里尔的建议和此库进行编码:http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
I want to take a screenshot and then send to webview a local path for it, so it will show it. I know that webview could take images from NSBundle but this screenshots are created dynamically - can I add something to it by code?
I was trying it like this:
UIGraphicsBeginImageContext( webView.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *file = @"myfile.png";
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *path = [directory stringByAppendingPathComponent: file];
[UIImagePNGRepresentation(viewImage) writeToFile:path atomically:YES];
NSString *function = [NSString stringWithFormat:@"loadImagePlease(%@);",path];
[ ad stringByEvaluatingJavaScriptFromString:function];
But loadImageFunction can't access it , with file:/// or file:// prefix. What's the solution?
Thanks for ANY help.
EDIT: added a missing line in code
SOLUTION: I used a Cyrille's advice and this library for encoding : http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其转换为 base64 数据 URI 方案 并通过 Javascript 发送,就像您当前所做的那样:
<代码>someDOMelement.src = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==)';
Convert it to a base64 data URI scheme and send that via Javascript as you're currently doing :
someDOMelement.src = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==)';
如果您不需要存储图像,您可以将其转换为
NSData
并直接加载UIWebView
,完全跳过 URL:更新:
因为您这样做 需要存储文件,使用 NSFileManager 获取目录的 URL,然后使用该 URL 写入和检索文件:
If you don't need to store the image, you can convert it to
NSData
and load theUIWebView
directly, skipping the URL altogether:UPDATED:
Since you do need to store the file, use
NSFileManager
to get the URL of your directory, then use the URL to write to and retrieve the file:您尚未将图像保存到该路径。
You haven't saved the image to that path.