I have a image say point.png in folder ABC in resourse.
I want to show that image in html
NSString *myFilePath= [[NSBundle mainBundle] pathForResource:#"Point1" ofType:#"png"];
NSMutableString *strForWebView = [NSString stringWithFormat:#"<html>"
"<head><b>%#</b></head><br>"
"<img src= %# height='30' width='30' />"
, [[play quotations] objectForKey:#"Heading"], myFilePath, .......
SO now what should i do so that it picks the path of image...in html from which it is loaded in web view...
pls help
Thanks
With your approach that is critically important to give the correct baseUrl to load the webView.
If you need the base url to be not local to load some external data, consider displaying images via base64 presentation, i gave the details how you can do it in this answer.
At the OP of this thread you will also see how the base url must be used if you want to display the local content only.
Related
I created an HTML file programmatically with images sources. The images are stored in the documents directory. The problem is that when I give the image source path as document directory path the images are not shown in Windows.
So, I choose the second option is to give only the image's name instead of whole path. Then it work well on Mac & Windows, but now images are not shown on the device's UIWebView.
Then I selected to open the HTML file in Safari insted of UIWebView, but the safari could not be opened because URL becomes NULL.
My code to open Safari is as follow:
NSLog(#"%#",documentsDirectory);
NSString *urlStr = [NSString stringWithFormat:#"%#/AccReport1.html",documentsDirectory];
NSLog(#"urlStr :: %#",urlStr);
NSURL *url1=[[NSURL alloc]init ];
url1 = [NSURL fileURLWithPath:urlStr];
NSLog(#"url :: %#",url1);
[[UIApplication sharedApplication]openURL:url1];
I want to mail the HTML file that contains images. And these HTML file and images are stored in document directory. I want to show this HTML file with images in windows browser, mac browser and iphone's safari.
How to do this?
One of the simple way to solve your problem is to keep Images and HTML file in same directory and refer those images in HTML files just by name and if you put them in a folder then give the complete path.
The images are not showing because the image path specified within the HTML code is not the documents directory path.
I suggest you might have to construct an html string and then subject the uiwebview to load that string
NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\" %#/image.png \"></img></body></html>",documentsDirectoryPath];
[webView loadHTMLString:htmlString baseURL:nil];
i've a problem.
I've an app that download rss feed from a website. It works all fine, but i want to set image in table view where the articles appear. How can i get image from RSS Feed? I need the code.
For title, description i've done it perfectly, but i'm not able to recover image.
P.S. : sorry for my english, i'm italian :)
Ok, so I presume you have the URL of the image. You can do this:
//Normal text setting code
//Normal text setting code
NSString *imageURLString = [rssFeed objectForKey:#img"];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
cell.imageView.image = [UIImage imageWithData:imageData];
That is the quick and filthy method. It's running in your UI thread etc, so it's going to be a terrible user experience. You can fiddle with performance on your own terms, but I'd suggest one of the following:
Load all your data asynchronously and fill the data as it becomes available
Load all of your data in a secondary thread, but show a loading spinner
For both of the above, you would put your images into an NSArray and then call them by doing cell.imageView.image = [myImageArray objectAtIndex:indexPath.row];
The best solution however (and most complicated) though is called 'lazy loading'.
If you want to do lazy loading (the way facebook and twitter do it) you should check this out: LazyTableImages.
Happy coding, Zane
There is a plugin for it.
WP RSS Images Plugin
http://wordpress.org/extend/plugins/wp-rss-images/
After downloading it and succesfully installing it you need to parse the enclosure tag.
It will be in enclosure element with the attribute of "url". If you need I can post the code also.
I display some html in my UIWebView and I have in my html references to images like: '/images/test1.gif' etc.
I display this in that way:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[myWebView loadHTMLString:someHtml baseURL:baseURL];
I've also added all required images to my Xcode project, into 'images' directory. The problem is, that, as far as I can see, the baseUrl ends with '/', and path to the images starts with '/' (it's absolute). All that makes my images doesn't appear.
I've noticed, that if I change, in html, path to images, to for example 'images/test.gif', the image will show up, but I want (if possible) to avoid changing all image paths in html from absolute to relative, becasue this html is imported from some database, and in case of another import I would have to change it again.
Have you tried clipping the slash from the end of baseURL? I should hope the web view is smart enough to add a slash where needed, despite blindly concatenating two strings that result in a double slash.
I'm developing an app that requires caching web pages (completely) along with their CSS files and images after saving the entire HTML of the page (going through the links to store each file along with the HTML file).
While viewing the HTML file offline, UIWebView takes a long time to load the page, given that I'm already offline, and the file is on disk along with its CSS and images.
I'm using this code to load the file:
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFilePath];
[wView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL fileURLWithPath:self.htmlFolderPath isDirectory:YES]];
Is there any other means of loading the file to the UIWebView that can load it faster?
P.S: it loads very fast on simulator (offline) but on device it takes a long time (considering its an already offline cached file)
Thanks for help.
Is your solution actually cache things other than the html file, meaning pictures, css, etc, AND relinking them in the html page? I am guessing that you are downloading and caching the html page and external resources then the UIWebView is loading that html and still going out to load the external resources. That would explain the fast performance on the simulator and the slower performance on device.
Here is a library designed to do exactly what you are trying to do: ASIWebPageRequest.
It should also be noted that it could simply be a case of disk i/o bottlenecking. I know on my project, I was loading large images in a uitableview and even after they were cached I noticed quite a bit of lag when pulling them off the disk because they were so big.
I've found certain kinds of CSS can grind WebView rendering to a halt. For example:
body { -webkit-box-shadow:inset 0 0 100px #222; }
This works great in the simulator, looks nice too. But on the phone (even the iPhone 4, iOS 4.2), a simple page will take 10sec to render.
Since this probably just takes time because it needs to parse and render the page, you may consider firing up the UIWebView in the background; i.e. added as a subview, but not visible.
Maybe the UIWebView is smart enough to know it doesn't need to do anything, but I suspect that at least html and css parsing is done right away.
If it doesn't do anything without being visible, reduce size to 1x1 and set opacity=0, and put that pixel some place where it can't interfere with touch event handling.
Not perfect but it may work.
I'm almost sure you will never be able to do this. The UIWebView just needs some time to process your webpage even when it's a local page.
Keeping that in mind you can try to preload the page before it's being shown. For example if you show it after a user presses a button, preload the page when you show the button instead of when the user actually presses the button. The user doesn't notice the slow loading, because it's being handled in the background so when the user presses the button the page is already been loaded.
I can give you an idea about alternative ways of loading HTML from file into the UIWebView. A small project I've got uses Objective-C as a pure wrapper for UIWebView
The project is here, but the main code is below: http://github.com/robb1e/iWeb
- (void)viewDidLoad {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html" inDirectory:#"."]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
}
I'm also exploring ways of improving the perceived performance by showing an image while the DOM is getting ready. Some answers I've had are here:
Displaying a background image on UIWebView
Just if in case someone happens the same to me...
I had a UIWebView that was loading a string html and every resource (js and css) was stored locally.
I've faced that loading the content with internet connection it was some kind of slow (1 or 2 seconds to load and appear the webview in my controller) but when I tried to load the same page WITHOUT internet connection it was fast, really fast.
I've remembered that my HTML template had this in the start <base href="http://somesite.com" /> that I've used to load some images whit relative paths in some contents.
Removing that work as charm. So that can be making your web view load slower even if you don't have any reference to extern content in your HTML.
Even in my case, while loading local html files to webview was taking too much time.
Try to load local html files as below, it worked for me:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"website_and_mobile_tnc-1" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[_TandCView loadHTMLString:[headerString stringByAppendingString:htmlString] baseURL:nil];
If you want to load using NSData, try to make baseUrl to "nil" in your code.
I have modified your code as below,
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFilePath];
[wView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:nil];
I didn't tried this, but please try and let me know.
i want to load just the html from the web and show the images from the local resource.
This helped me a lot, but is the path to the resource on every device the same? Or does it depend on an installation path or similar?
Load Content in the UIWebView on the Iphone
the path to resources is different on every device, that's why you use the function
[[NSBundle mainBundle] pathForResource:nameOfImage ofType:#"jpg"]
the html loading from the web you can do all kinds of ways. The easiest is just:
NSString *myHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://web.site.com/file.html"]];