简体   繁体   English

如何从ios webview读取文档目录中的图像文件

[英]How to read image file at documents directory from ios webview

I am downloading image file and write to documents directory like below: 我正在下载图像文件并写入文件目录,如下所示:

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];

NSString  *objURL = @"http://test.test.com/file/image.png";
NSURL     *objurl = [NSURL URLWithString:objURL];
NSData    *imgData = [NSData dataWithContentsOfURL:objurl];

NSString *imgFile = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"image.png"];
[imgData writeToFile:imgFile atomically:YES];

In my webView, I loaded a html file from my bundle resource to use in my webView, but how do I have the tag in html to read the image.png in documents directory? 在我的webView中,我从我的bundle资源中加载了一个html文件,以便在我的webView中使用,但是如何在html中使用标签来读取文档目录中的image.png?

<html>
<body>
    <img src="../what/is/the/path/to/use/image.png" />
</body>
</html>

You could give the URL for this image using an absolute path with a file:// scheme: 您可以使用带有file:// scheme的绝对路径为此图像提供URL:

NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)[0];
NSString *filePath = [NSString stringWithFormat:@"file://%@/image.png", documentsDirectory];

You could then run some javascript to update the src tag to update it to the new path: 然后,您可以运行一些javascript来更新src标记以将其更新为新路径:

NSString *javascript = [NSString stringWithFormat:@"var imageElement = document.getElementById('localFile'); imageElement.setAttribute('src', '%@');", filePath];
[self.webView stringByEvaluatingJavaScriptFromString:javascript];

In your HTML the path would look like something like: 在您的HTML中,路径看起来像:

<html>
    <body>
        <img id="localFile" src="file:///var/mobile/Applications/3D7D43E8-FA5E-4B19-B74C-669F7D1F3093/Documents/image.png" />
    </body>
</html>

Instead of evaluating JavaScript, I just write down a placeholder in my html content, and replace it directly w/ the content needed: 我只是在我的html内容中写下一个占位符而不是评估JavaScript,而是直接用所需内容替换它:

NSString *htmlFilePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
  // handle error
  return;
}

NSString *imageFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"sample_image.png"];
NSURL *imageFileURL = [NSURL fileURLWithPath:imageFilePath];
NSString *sampleImageReplacement =
  [NSString stringWithFormat:@"<img src=\"%@\" />", [imageFileURL absoluteString]];

// Replace the placeholder w/ real content,
//   and of course, we can also replace it w/ empty string
//   if the image cannot be found.
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"%sample_image%"
                                                   withString:sampleImageReplacement];
...
[webView loadHTMLString:htmlString baseURL:nil];

You can load the images from the app bundle but you can not load the images from the documents directory directly. 您可以从应用程序包中加载图像,但无法直接从文档目录加载图像。

But there is a work around. 但是有一个解决方法。

Use CocoaHTTPServer to create a InAppHttpServer and you can access the images in documents directory using http URL in web view. 使用CocoaHTTPServer创建InAppHttpServer,您可以在Web视图中使用http URL访问文档目录中的图像。

or else 要不然

Convert the image into base64 and load the image using evaluatejavascript of webview 将图像转换为base64并使用webview的evaluatejavascript加载图像

document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' );

You can not bind image source from beginning. 您无法从头开始绑定图像源。 You will have to do it via JavaScript. 你必须通过JavaScript来做到这一点。

You need to pass the path of the image to UIWebView by evaluating JS. 您需要通过评估JS将图像的路径传递给UIWebView。 Here you can pass the path of your image and JS will load the image into respective HTML tag. 在这里,您可以传递图像的路径,JS会将图像加载到相应的HTML标记中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本地HTML中显示保存在文档目录中的图像? 的iOS - How to show an image saved on Documents Directory inside local HTML? iOS 迅速将html文件从ios上的文档目录加载到UIWebview - load html file to UIWebview from documents directory on ios with swift iOS 13 Swift 5 wkwebview - 将文档目录中的图像显示到 wkwebview 真实设备中 - iOS 13 Swift 5 wkwebview - display image from documents directory into wkwebview real device 无法从文档文件夹加载到iOS上的WebView中 - Can't load from documents folder into webView on iOS 在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接 - ios-css and js not working linked in html file when opening in UIWebview from Documents directory Android:从webview读取图像,将图像写入webview - Android: read image from webview, write image to webview 如何使用NodeJS从HTML文件读取图像? - How to read image from HTML file with NodeJS? iOS:从文档目录中读取html文件 - iOS: read html files from document directory wkwebview - 将文档目录中的图像显示到本地 html 中 - wkwebview - display image from documents directory into local html 在iPhone上的UIWebView中显示文档目录中的本地HTML文件 - Display Local HTML file from documents directory in a UIWebView on iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM