How to read image file at documents directory from ios webview

后端 未结 4 604
余生分开走
余生分开走 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: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:@"", [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];
    

提交回复
热议问题