将本地图像加载到 UIWebView 中

发布于 2024-11-28 06:01:49 字数 1145 浏览 0 评论 0原文

我想截取屏幕截图,然后将其本地路径发送到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℡Ms空城旧梦 2024-12-05 06:01:49

将其转换为 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==)';

耳根太软 2024-12-05 06:01:49

如果您不需要存储图像,您可以将其转换为 NSData 并直接加载 UIWebView,完全跳过 URL:

NSData *data = UIImagePNGRepresentation(viewImage);
[myUIView loadData:data MIMEType:@"image/jpeg" textEncodingName:@"UTF-8" baseURL:nil];

更新:

因为您这样做 需要存储文件,使用 NSFileManager 获取目录的 URL,然后使用该 URL 写入和检索文件:

NSURL *pathUrl = [[NSFileManager defaultManager] URLForDirectory:directory inDomains:NSDocumentDirectory appropriateForURL:nil create:NO error:nil];
NSURL *fileUrl = [pathUrl URLByAppendingPathComponent:file];
[UIImagePNGRepresentation(viewImage) writeToURL:fileUrl atomically:YES];
NSString *urlString = [fileUrl absoluteString];

If you don't need to store the image, you can convert it to NSData and load the UIWebView directly, skipping the URL altogether:

NSData *data = UIImagePNGRepresentation(viewImage);
[myUIView loadData:data MIMEType:@"image/jpeg" textEncodingName:@"UTF-8" baseURL:nil];

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:

NSURL *pathUrl = [[NSFileManager defaultManager] URLForDirectory:directory inDomains:NSDocumentDirectory appropriateForURL:nil create:NO error:nil];
NSURL *fileUrl = [pathUrl URLByAppendingPathComponent:file];
[UIImagePNGRepresentation(viewImage) writeToURL:fileUrl atomically:YES];
NSString *urlString = [fileUrl absoluteString];
比忠 2024-12-05 06:01:49

您尚未将图像保存到该路径。

You haven't saved the image to that path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文