How to read image file in document directory from ios website

I upload the image file and write to the document directory as shown 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 downloaded the html file from my bundle resource for use in my web browser, but how do I get the tag in html to read the image.png file in the document directory?

 <html> <body> <img src="../what/is/the/path/to/use/image.png" /> </body> </html> 
+4
source share
4 answers

You can specify the URL for this image using the absolute path using the file:// scheme:

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

Then you can run some javascript to update the src tag to update it to a new path:

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

In your HTML, the path will look something like this:

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

Instead of evaluating JavaScript, I just write the placeholder in my html content and replace it directly with the content:

 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]; 
+1
source

You can download images from a set of applications, but you cannot download images from a document catalog directly.

But there is work around.

Use CocoaHTTPServer to create InAppHttpServer, and you can access the images in the document directory using the http URL in the web view.

or more

Convert image to base64 and upload image using evaluatejavascript

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

You cannot link an image source from the beginning. You will need to do this via JavaScript.

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

0
source

Source: https://habr.com/ru/post/1489951/