Create a document with embedded images - html

I'm trying to create a document (i.e. .doc,.html) with embedded images with objective-c.
The images are stored locally on the iPhone.
For example, I've created an html using img tag and the file uri scheme. Then I saved the html code on a .doc file. But the images are not displayed.
The same thing happens if I save the html code as an .html file.
[body appendString:#"<img src='"];
[body appendFormat:#"file:/%#", absolutePath];
[body appendString:#"' />"];
where absolutePath is the path of the image on the device (I printed it and it's right).
Any suggestion?

Unfortunately you can't link to local files in HTML directly. There is a trick however: using a local substitution cache that redirects the download of the image and returns a local image instead. If the code creating the document and the cache both agree on what URL points to which image, you can get local images to appear in your HTML.
Refer to this excellent blog post to learn about how to implement such a substitution cache: http://cocoawithlove.com/2010/09/substituting-local-data-for-remote.html

Related

Can I link .png images I have stored in res/mipap to html files I have stored in android_asset, so WebView shows both text and images in an html file?

I have an Android app which uses the WebView function, it opens html files inside my app from a local host that have been stored in the android_asset folder when prompted. However, the WebView function only shows the text in the html file, it does not show the images that I have linked into the code of the html document, these are stored in res/mipmap. It is important to me that my app can show both text and images when I use WebView to open the html file. It is also important that the html files and the images are stored in a place that means they will be added to the installable (so that the user does not have to be connected to the internet to use the App or move any files around themselves). Is it possible to link an image stored on a local host in something like android_asset or res/mipmap so that I can see both text and the image when I open the html file in WebView?
The link to the images that does not work is in res/mipmap, it is: img src = "file:///mipmap-hdpi/car.png" alt = "Test Image"
I made a new folder inside android_asset called imagepng. I then put the .png's in this folder (so the link to the image in the htlml file looks like ). The html file now calls the .png image from this place and I can see it on the app. I do not know yet if this makes it into the installable so it can be run locally from the
smart phone..........My original mistake was trying to put the images directly into android_asset as it does not allow this.

Why is my html image scr searching into the url instead of my directory?

I have an html file in which I would like to display an image called plot.png with the line <img src="plot.png" alt="Stock price vs. predictions graph">. On my website, I only see the alt text, meaning that my image did not load properly. In my command prompt output I see that I have a get request to /mysite/home/AAPL/plot.png, which is extremely frustrating because this means that when I search for the image this code is just placing it in the url (which is localhost../mysite/home/AAPL). I have tried putting plot.png in the same working directory as my html file as well as trying the absolute path to plot.png starting with C:, but nothing seems to get the search out of the url. Please help, thanks!
If it helps, im using Django
You can put the image in the same working directory (in the same folder as your html file) and then use
<img src="./plot.png" alt="Stock price vs. predictions graph">
The "./" is important as it signals that the image is in the current folder.
You could also use a website like www.linkpicture.com to generate a link to host your image and then use that link in your img
Some web browsers automatically disable images from loading. Fixing this could be as simple as selecting “show all images” from the browser's settings menu. It's also worth checking if the device you're using has security software or extensions that could block images.
Again you can use this tag for .png type photo
<img src="exampel.end">
//use extension type .end instead of .png
I forgot to mention that I was using the Django framework and the html templates work much differently than regular html files do. In Django you must put the image in a static folder and then call if with Jinja like so: <img src="{% static 'mysite/image.PNG' %}">

Add image data to html file?

My goal is to create one HTML file, that contains everything that it needs for it's proper functioning.
I've added my .css's content to the html head between <style> tags, my javascript just before the closing body tag between <script> tags, but how can I do the same thing with my images?
The "img" tag's "src" obviously won't work if I copy my HTML to another computer.
Any suggestions?
Convert image to base64 (however it is not recommended for large images)
For example
<img src="data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5OSYdGFl5jo+Pj/+pqcsTE78wMFNGQLYmID4dGPvd3UBAQJmTkP+8vH9QUK+vr8ZWSHpzcJMmILdwcLOGcHRQUHxwcK9PT9DQ0O/v70w5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UNrkji9P55sF/ogxw5ZkSqIDaZBV6aSGYq/lGZplndkckZ98xoICbTcIJGQAZcNmdmUc210hs35nCyJ58fgmIKX5RQGOZowxaZwYA+JaoKQwswGijBV4C6SiTUmpphMspJx9unX4KaimjDv9aaXOEBteBqmuuxgEHoLX6Kqx+yXqqBANsgCtit4FWQAEkrNbpq7HSOmtwag5w57GrmlJBASEU18ADjUYb3ADTinIttsgSB1oJFfA63bduimuqKB1keqwUhoCSK374wbujvOSu4QG6UvxBRydcpKsav++Ca6G8A6Pr1x2kVMyHwsVxUALDq/krnrhPSOzXG1lUTIoffqGR7Goi2MAxbv6O2kEG56I7CSlRsEFKFVyovDJoIRTg7sugNRDGqCJzJgcKE0ywc0ELm6KBCCJo8DIPFeCWNGcyqNFE06ToAfV0HBRgxsvLThHn1oddQMrXj5DyAQgjEHSAJMWZwS3HPxT/QMbabI/iBCliMLEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==">
To convert image to base64, you can use HTML5 canvas toDataUrl.
Just upload your images to the site imageBB copy the source link paste it into your HTML file. This way the images will be loaded directly from the server and you don't need to copy the images in every system you need to load your site.
Pros:
1)You can load the image from the server directly.
2)No need to copy the images repeatedly in every system
cons
1) The system which you want to load the site needs to have an internet connection.

Unusual issue with HTML image and 'file://' specified src

I'm loading an image on the page with a 'file:///some_dir/image.jpg' src path. I can access the image in a regular tab using this path. Also, saving the page as HTML and using this path for the image works. However, the image does not load on the live page. In chrome it shows part of the alt text, and in firefox it shows a narrow strip. I have tried changing width and height but to no avail. Is there something I'm missing?
<img title="Click to enlarge" src="file:///Users/Aram/uploads/profile.image.985b0f707d972bf3.4372696242656464696e67616e645465657468696e67437269625261696c436f7665722e6a7067.jpg" class="profile-image">
EDIT:
I noticed I am getting this in the console:
Not allowed to load local resource
Is there any way around this?
EDIT 2:
Since I could not access the image through an http path, I have decided to read it in as base64 data. For anyone else using web2py or another Python framework:
# Load the image data
import os
path = os.path.join(request.folder, 'uploads', filename)
data_uri = open(path, 'rb').read().encode('base64').replace('\n', '')
data = 'data:image/png;base64,%s' % data_uri
return html.IMG( _src=data, _class='profile-image', _title='Click to enlarge' )
Websites are not allowed to use local files on the user's computer. Use a relative path to from the html file's directory.
You can also encode and embed the image directly:
How to embed the encoded stuff: http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html
Python Encoding Instructions: http://www.daniweb.com/software-development/python/code/216635
The problem is you are trying to load a file directly off of a clients computer. Browsers prevent this.
You can read about the exact details here:
http://en.wikipedia.org/wiki/Same_origin_policy
It is called the Origin Policy. It prevents malicious sites from directly loading files off of a clients computer. Try using a relative path from your page to display the image.
In some situations (rare) I've used a light web server to host the site so that I could load the files from the server (as opposed to having it load off of what the browser sees, as a clients computer).

iphone uiwebview download complete page with CSS and Images

In my app there's a uiwebview that loads a URL.
I am using the following line to save the HTML of the page loaded locally to be able to view it offline:
NSString* html=[webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].innerHTML"]
The problem is that only the HTML of the document gets saved. I want to save also the images and the CSS along with the HTML so that the user see the page as if they are online.
Just like "save web page complete" or something like that, that we're used to in the browsers.
There is no easy way. Regex the HTML using RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/index.html) and snag all the urls to .jpg,.gif,.png, and .css and .js and whatever all else you need.
alternately, call:
NSString* imgUrls=[webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('img')"]
or something like that, I'm no javascript whizz... and then deal with whatever all that returns ;)
Sorry. It's a pain in the rearheinie.
edit:
Save all the img's on the iphone, also save the html file. When you want to reload the page, load the html from a file into a string, and then use
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
to load the HTML string. baseURL is used to specify the directory or site the webview will imagine the html string you hand it is located. All URLS will be relative to that.
Note, of course that this will not work very well for absolute URLs, only for relative ones. So this, in your html file, will monkey things up:
<img src="http://google.com/f/r/i/g/img.gif">
while this would be ok:
<img src="f/r/i/g/img.gif">
Again, this whole solution is mucky.
You might look into a pre-existing open source recursive html spider. I think wget does what you want, but I doubt it can be compiled for iPhone without a -lot- of hassle.
I didn't have time to check, but ASIWebPageRequest seams very promising. It states it can "Store a complete web page in a single string, or with each external resource in a separate file referenced from the page"
ASIWebPageRequest1
I will be using it on one of my projects, and then update thread.
Gonso