How do i convert UIWebView to bitmap - ios

Hi everybody i'm stuck with trying to convert my UIWebView, in which i'm currently displaying a ppt file, to a bitmap image.
I have this code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 708)];
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
NSLog(#"Ouverture du contexte image");
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The webview is actually well displayed but I didn't manage to create it's render to an image,
If anybody knows where is the issue ;)
Thankss

There's nothing wrong with your screenshot-taking code — you're just using it prematurely.
[webView loadRequest:request]; initiates an asynchronous client request. That means you're taking a screenshot of webView before it even loads its contents.
Move your screenshot-taking code to UIWebViewDelegate's method webViewDidFinishLoad:. Of course you're going to have to assign a delegate to your webView to achieve this.

Related

How to load, scroll and pinch and zoom *.svg image in iOS device using Objective -C

In my ios application, I am displaying a *.svg file as below:
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"svg"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:path];
NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
[_webView setScalesPageToFit:YES];
[_webView loadRequest:req];
This works fine as I can load pinch and zoom the file on device. But now I need to highlight and area of image (say a room of a building or well of a garden). How can I achieve this? I have tried SVG Kit, but I was not able to find the touch-highlight area property. I've also used FSInteractiveMapView. But is does not allow me to pinch and zoom or scrol through the svg image. Please help me.

iOS Load Image in UIWebView

Desperately trying to figue out how to load a local image (saved in local xcode bundle) into the following HTML through a UIWebView. I have spent countless hours following other guides but nothing seems to work, all throw erros. Am trying to add a local image in the following NSString where I have written IMAGEGOESHERE -
{
[super viewDidLoad];
self.title = _restaurant.title;
[[self navigationController] setNavigationBarHidden:NO animated:NO];
self.activityIndicatorView.hidden = NO;
[self.activityIndicatorView startAnimating];
[self loadImageInNewThread];
NSString *webViewContent = [NSString stringWithFormat:#"<html><head><style>* {font-family: Helvetica}</style></head><body><center>%# - %#<br><br><b>Restaurant:</b>%#</b><br>IMAGEGOESHERE<br></center></b></body></html>", _restaurant.openingTime,_restaurant.closingTime, _restaurant.name];
[self.webView loadHTMLString:webViewContent baseURL:nil];
}
I hope you can help as its driving me mad!
You need to reference first the path of your image:
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"yourimage" ofType:#"png"];
and then specify your path in your webViewContent along with the size of your image:
NSString* webViewContent = [NSString stringWithFormat:
#"<html>"
"<body>"
"<img src=\"file://%#\" width=\"200\" height=\"500\"/>"
"</body></html>", pathImg];
This code should work,
Just replace yourFile with the name of your PDF, and webView with the name of your webView.
Then Replace ofType to the extension of your image.
//PDF View
//Creating a path pointing to a file.pdf
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"pdf"];
//Creating a URL which points towards our path
NSURL *url = [NSURL fileURLWithPath:path];
//Creating a page request which will load our URL (Which points to our path)
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//Telling our webView to load our above request
[webView loadRequest:request];
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"yourimage" ofType:#"png"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html>"
"<body>"
"<img src=\"file://%#\" width=\"200\" height=\"500\"/>"
[webView loadHTMLString:webViewContent baseURL:nil];
You must add the last line with a base url of nil

Run HTML5/CSS3 in UIWebView

I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com

Proper way to load in html file from resources into UIWebView?

I thought it would be easy to load in a html file from my resources into a UIWebView, but it looks like it requires something extra that I'm not doing. So far I have in my viewDidLoad in my controller:
NSURL *url = [NSURL URLWithString:#"radialGradient.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
webView.frame = CGRectMake(0, 0, 768, 1024);
I've tried regular web addresses (google.com) and it works fine. But with the file in my sources it just renders a white blank screen.
Two things:
1)
What you have now is not a URL. It's just a filename. You need to be prepending a "file:///" in front of that radialGradient.html.
2)
And, you'll likely also need to put in a resolvable path to radialGradient.html Right now your app doesn't know where to look for it.
Something like:
NSURL * resourcePathURL = [[NSBundle mainBundle] resourceURL];
if(resourcePathURL)
{
NSURL * urlToLoad = [resourcePathURL URLByAppendingPathComponent: #"radialGradient.html"];
if(urlToLoad)
{
NSURLRequest * req = [NSURLRequest requestWithURL: urlToLoad];
[webView loadRequest: req];
}
}

View Html in ios/ipod Apps

Hi i want to make a few http request and generate html from within my ipod app. I hear i want a WebView to view the html as if it were a browser. So my question is how do i make the http request? How do i parse json (or xml) and how do i save/load data? I figure i can use webstorage to load/save the data however i am thinking since its an ipod app i have a better way of storing it?
Here's a snippet to load a local html web page contained inside your app bundle:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"mywebpage" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Another alternative is to embed a local web server running in a background thread, and use a URL to localhost.
A UIWebview supports HTML5 local storage.
Here is the working code snippet. for iPod/iPhone/iPad;
Works with iOS7 and iOS6.. I have not tested in others...
UIWebView *_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

Resources