Am displaying a Image on a web view and it doesn't fit the web view completely and shows blank white space around the border.How can i fit or scale the image completely to the size of web view?
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.backgroundColor=[UIColor blackColor];
self.webView.delegate = self;
[self.webView setScalesPageToFit:YES];
Try this html code snippet
<html><body><img src='%#' width='100%' height='100%'></body></html>
It will fill the web view completely without any white space.
try to load like this
- (void) showImageWithLoadHTMLString {
// Create URL string for image file location
NSURL *imageURL = [NSURL fileURLWithPath: path];
// Create HTML string from image URL
NSString *htmlString = #"<html><body><img src='%#' width='900'></body></html>";
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL];
// Load image in UIWebView
self.webView.backgroundColor=[UIColor blackColor];
self.webView.scalesPageToFit = YES;
[self.webView loadHTMLString:imageHTML baseURL:nil];
}
Related
I have a PDF file stored in my local applications directory. I wrote the following code years ago to display the pdf in a webView. It has worked flawlessly, but no longer works. It instead displays a white screen.
NSString* pdfFileName = [self getPDFFileName];
WKWebView* webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *url = [NSURL URLWithString:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
I have since tried other methods of WKWebView such as:
NSData *data = [NSData dataWithContentsOfFile:pdfFileName];
[webView loadData:data MIMEType:#"application/pdf" characterEncodingName:#"" baseURL:[NSURL URLWithString:pdfFileName]];
or
[webView loadFileURL:url allowingReadAccessToURL:url];
but these don't work either.
What changed that is preventing a local pdf from displaying?
You need to use:
[NSURL fileURLWithPath:<valid_file_path>]
Always use your file browser to confirm both the file path and the path itself are valid in case of any issues.
I have a webView and when the content initially loads it looks perfect and fits nicely but as soon as it completes loading it automatically zooms way in. Is there a way to stop this? Ive tried more combinations than I can possibly post here.
Many Thanks.
The Code I'm using is pretty straight forward:
NSString *urlString = #"some_url";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[cell.webView loadRequest:request];
Here's what you do:
In your UI controller that owns the web view, make it a UIWebViewDelegate. Then where you set the URL to load, set the delegate as the controller:
NSString *urlAddress = #"http://dl.dropbox.com/u/50941418/2-build.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
And finally implement the webViewDidFinishLoad: to correctly set the zoom level:
This option will applicable from iOS 5.0 and >
- (void)webViewDidFinishLoad:(UIWebView *)theWebView{
CGSize contentSize = theWebView.scrollView.contentSize;
CGSize viewSize = theWebView.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
}
I hope this helps...
Option B, you can try to alter the HTML (this example does the job but is less than perfect from an HTML parsing standpoint. I just wanted to illustrate my point. It does work for your example, and probably most cases. The inset of 40 can probably be detected programmatically, I didn't try to research that.
NSString *urlAddress = #"http://dl.dropbox.com/u/50941418/2-build.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSString *html = [NSString stringWithContentsOfURL:url encoding:[NSString defaultCStringEncoding] error:nil];
NSRange range = [html rangeOfString:#"<body"];
if(range.location != NSNotFound) {
// Adjust style for mobile
float inset = 40;
NSString *style = [NSString stringWithFormat:#"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
html = [NSString stringWithFormat:#"%#%#%#", [html substringToIndex:range.location], style, [html substringFromIndex:range.location]];
}
[webView loadHTMLString:html baseURL:url];
I'm using a UIWebView to display a mobile website on my iPhone. The UiWebView is smaller than the display of the iPhone.
My Problem is that the content size of the website (a mobile website based on JQuery mobile) doen't fit the size of the web view.
Just an example with a small web view trying to display the google mobile website:
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView sizeToFit];
Primarily : set the delegate to the WebView
In .h file
#interface WebViewViewController : UIViewController<UIWebViewDelegate>
In .m file
webView.delegate =self;
webView.scalesPageToFit = NO;
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
add these to your code for your webview :
[webView setScalesPageToFit:YES];
[webView setContentMode:UIViewContentModeScaleAspectFit];
In my app, I have a view controller that contains a UIWebView. This View Controller has the UIWebView as an IBOutlet. When loading the view I just get a white screen with my navigation bar at the top. Here is the code for loading the UIWebView. Originally my completeURL wasn't being formatted correctly and was just null. No information I can scour through online seems to be able to help me with this issue.
-(void) viewWillAppear:(BOOL)animated
{
NSString *stringURL = [NSString stringWithFormat:#"www.ncbi.nlm.nih.gov/pubmed/?term=%#", _URL];
NSURL *completeURL = [NSURL URLWithString: [stringURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:completeURL];
[_webView loadRequest:request];
NSLog(#"%#", request);
}
once check this line may be problem is here replace this once and check it,
NSString *stringURL = [NSString stringWithFormat:#"http://www.ncbi.nlm.nih.gov/pubmed?term=%#", _URL];
I think the problem could be in:
NSURL *completeURL = [NSURL URLWithString: [stringURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
check intermediate strings
I'm trying to run a web form on an ipad that has no web connection. So far i've successfully been able to load the form into safari through the use of an app that takes a html file and displays it in a browser.
However, the data store sql component of html 5 just doesn't seem to work. I copied the code from html5rocks.com
http://playground.html5rocks.com/#async_transactions
Seems to work everywhere but in my application.
So my app is very simple, it just loads a web browser... here's the relevant code
- (void)viewDidLoad {
// Example 1, loading the content from a URLNSURL
//NSURL *url = [NSURL URLWithString:#"http://dblog.com.au"];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[webView loadRequest:request];
NSString *webPage = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *webData = [NSData dataWithContentsOfFile:webPage];
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
// NSString *HTMLData = #"
// <h1>Hello this is a test</h1>
// <img src="sample.jpg" alt="" width="100" height="100" />";
// [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadHTMLString:webPage baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:webPage isDirectory:NO]]];
NSString *baseStr = [NSString stringWithFormat:#"file:/%#//",imagePath];
NSURL *baseURL = [NSURL URLWithString: baseStr];
NSLog(#"%#",baseStr);
//webView.scalesPageToFit = YES;
//webView.multipleTouchEnabled = YES;
/*for (id subview in webView.subviews){
if([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
}*/
[webView loadData:webData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseURL];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.cyberdesignworks.com.au/clients/ideatoaction"]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Test" ofType:#"svg"]isDirectory:NO]]];
[super viewDidLoad];
The problem was that Xcode tries to compile my js files.
DAMN YOU APPLE!
anyway look on the right in the list of items in xcode when you have a project open
look for targets, it has a picture of a bow and arrow target, or like a red and white dart board. open that up, look at compiled resources. Move any js files from there to the copy bundled resources.
Bam! it should all work as intended now.