How to read image file at documents directory from ios webview

后端 未结 4 595
余生分开走
余生分开走 2020-12-18 09:24

I am downloading image file and write to documents directory like below:

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDo         


        
相关标签:
4条回答
  • 2020-12-18 09:32

    You can not bind image source from beginning. You will have to do it via JavaScript.

    You need to pass the path of the image to UIWebView by evaluating JS. Here you can pass the path of your image and JS will load the image into respective HTML tag.

    0 讨论(0)
  • 2020-12-18 09:39

    Instead of evaluating JavaScript, I just write down a placeholder in my html content, and replace it directly w/ the content needed:

    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];
    
    0 讨论(0)
  • 2020-12-18 09:44

    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.

    or else

    Convert the image into base64 and load the image using evaluatejavascript of webview

    document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' );
    
    0 讨论(0)
  • 2020-12-18 09:47

    You could give the URL for this image using an absolute path with a file:// scheme:

    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:

    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>
        <body>
            <img id="localFile" src="file:///var/mobile/Applications/3D7D43E8-FA5E-4B19-B74C-669F7D1F3093/Documents/image.png" />
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题