I am trying to display the image in UIWebView and added uiwebiview into the uiscrollview as i wanna give the page controller effect:-
UIWebView *webview_obj=[[UIWebView alloc] initWithFrame:CGRectMake(xcorrd,0,200,320)];
NSString *html = [NSString stringWithFormat:#"<html><body><img src='%#' width='100%' height='100%'></body></html>",imageStr];
[webview_obj loadHTMLString:html baseURL:nil];
webview_obj.scalesPageToFit=YES;
[scroll_views addSubview:webview_obj];
My problem is :-
When i try to load the image of sizd 200X320. the image come in perfect size.but some white portion(approximate 30-40pixel) will added on the bottom and side of the webview.
and automatically the content size get increase and give the scroll.
I am uploading the screenshot for the output i am getting:-
Black potion in the webview part.and white is somehting which get automatically added to the image..
the size of the image i am using is :- 200 :- with and 320 :- height
Thanks & Regards
shweta
Added CSS STYLE in the HEAD tag..
NSString *html = [NSString stringWithFormat:#"<html><head><style>body{padding:0; margin:0;}</style></head><body><img src='%#' width='100%' height='100%'></body></html>",imageStr];
Related
In an iphone app i have a webview in which i want to preview some image downloaded from internet, my problem is that the some images are not viewed as to fit in the frame of webview, but most do. I think this is due to the fact that those images are too large. Am i doing something wrong? Please help
What i want is simply loading image in webview to fit the frame of webview. You can provide me some other code, but need to be regarding webview not imageview.
Here is the code i am using.
self.documentData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.folio3.com/corp/wp-content/uploads/2013/02/Entrance_A-3.jpg"]];
self.webView.scalesPageToFit = YES;
[self.webView loadData:self.documentData MIMEType:#"image/jpeg" textEncodingName:#"utf-8" baseURL:nil];
Plus here is the screenshot of output (clearly shows scroll indicators i-e image is not fit in webview's frame)
First Check and set frame of UIWebView in viewDidLoad: like bellow..
webView.frame = self.view.frame;
after use this Delegate method and set scalesPageToFit property like bellow..
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
webView.scalesPageToFit = YES;//set here
return YES;
}
This solution builds on the answer from Ammar Hasan. Instead of the original...
NSData *data = [NSData dataWithContentsOfURL:imageURL];
[self.webView loadData:data MIMEType:#"image/jpeg" textEncodingName:#"utf-8" baseURL:nil];
...you can do this:
NSString *photoWrapper = [NSString stringWithFormat:#"\
<html>\n\
<head>\n\
<meta name=\"viewport\" content=\"width=device-width\" />\n\
<style type=\"text/css\">\n\
body {\n\
margin: 0;\n\
padding: 0;\n\
}\n\
img {\n\
width: 100%%;\n\
}\n\
</style>\n\
</head>\n\
<body>\n\
<img src=\"%#\" />\n\
</body>\n\
</html>\
", URL];
[self.documentView loadHTMLString:photoWrapper baseURL:nil];
Now the image is scaled to the width of the web view, even when the web view is less than half the width of the image, which seems to be a limit that UIWebView imposes otherwise. The image can still be scaled by the user, which I wanted. You can override that by adding minimum-scale and maximum-scale to the viewport meta tag.
Try using html content for WebView, like the following code
<html>
<head>
<title>Title</title>
</head>
<body style='margin:0px; padding:0px;'>
<img src='<your_image_url>'
style='max-width:320px; max-height:460px;'
alt='Your_Image_Name'/>
</body>
</html>
To re-size image use CSS (cascaded style sheet) styling, play with the these style properties in image style attribute
max-width, min-width, width, max-height, min-height & height
I am using a UIWebView to render html content. I have done the following to enable the pinch zoom on the UIWebView:
self.webView.scalesPageToFit = YES;
This has enabled me to enable pinch zoom and scrolling over the content of the .html page being rendered in the UIWebView.
But, there is a limit upto which I zoom in. I have found in the following link regarding the level of zooming in available in the following link:
UIWebView Pinch Zoom
Is there a way we can zoom beyond the limit of 100%?
Is there a different approach we can follow?
EDIT: I have tried setting the minimumZoomScale and maximumZoomScale of the ScrollView property of the UIWebView object, but still, that has not worked out. What could I be missing?
try this using javascript using initial-scale=1.0; maximum-scale=5.0 here set your value
ViewDidLoad method
NSString *embedHTML = #"<html><body bgcolor=#EAEAEA><font face='Myriad Pro' size='3'><meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable=YES'/>";
NSString *endHtml=#"</font></body></html>";
NSString *strDes=[NSString stringWithFormat:#"%#%#%#",embedHTML,YourWebviewDescription,endHtml];
[self.webViewObj loadHTMLString:strDes baseURL:nil];
NSString *string = [NSString stringWithFormat:#"document.body.style.zoom = %f;", fontSizeSlider.value]; // I used sliderControl and its value is 1 to 5.
[webViewObj stringByEvaluatingJavaScriptFromString:string];
When we change the slider position (drag the slider)
-(IBAction)sliderValueChanged:(UISlider*)sender
{
int font=(int)fontSizeSlider.value;
NSLog(#"font %d",font);
NSString *string = [NSString stringWithFormat:#"document.body.style.zoom = %f;", fontSizeSlider.value];
NSLog(#"string=%#",string);
[webViewObj stringByEvaluatingJavaScriptFromString:string];
}
I would like that my uiwebview change gif image when orientation changes so initially
i'm using the following code to load the first image :
NSString * url = #"gif image url...";
int width = width of image;
int height = height of image;
NSString * htmlCode = [NSString stringWithFormat:#"<html><head><body leftmargin=\"0\" topmargin=\"0\"><img id=\"gifimg\" src=\"%#\" width=\"%i\" height=\"%i\"></body></html>", url, width,height];
[tmpWebView loadHTMLString:htmlCode baseURL:nil];
and i put the following code in orientationChanged function:
NSString url = #"portrait image url...";
int width = width of portrait image;
int height = height of portrait image;
if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) {
url = #"landscape image url...";
width = width of landscape image;
height = height of landscape image;
}
NSString * jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('img')[0].style.width= '%dpx';document.getElementsByTagName('img')[0].style.height= '%dpx';document.getElementsByTagName('img')[0].src= '%#'", width,height, url];
[webview stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
[webview setBackgroundColor:[UIColor clearColor]];
[webview setOpaque:NO];
So the image work fine at first display and when i rotate the device, the (landscape or portrait) the image appear but a part of it was truncated.(after each rotation a part of image is ignored.....), any idea?
Thanks for help
That's because when you its loading the nib file as indicated in the xib file the uiimageview is most probably put at the end, so I guess you need to modify the frame coordinates in order to show the who image. if you want to test this theory, try to put in the xib file at the end of the image a button and run the application and rotate the device to show in lanscape, if my theory was correct the button won't show.
I hope this helps! good luck!
It's all about the orieitation issue,i guess so.
here you should do thing like
1)whenever orientation chnages get that point and recognise the orientation
suppose you got portrait then call your method which do the setUp of setting new image for portrait mode and do same vice versa.
Here is my logic just see it Carefully.
Below Method Called Whenever the Orientation gets Chnaged.Here You just need To allow To Chnage tHe orientation as you asked you need to set the Image for Landscape too.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation ==UIInterfaceOrientationPortrait||toInterfaceOrientation==UIInterfaceOrientationMaskPortraitUpsideDown);
{
[self setImagesForPortraitView];
}
else
{
[self setImagesForLandscapeView];
}
//setImagesForPortraitView and setImagesForLandscapeView would be your custom method which setUP the Images for different orientation
//So here as Orientation chnages you can easily set the new image to Webview as you said.
}
Note:Here I am just Posting this Answer Only For Your Current REq i don't know how u r managing the Orientation ...
I hope it may helps you.
I am creating a webview to display some text in my app.
CGRect frame = CGRectMake(lblContent.frame.origin.x,
lblContent.frame.origin.y,
lblContent.frame.size.width,
lblContent.frame.size.height);
NSString *htmlText = HTML_DIV_TAG;
htmlText = [htmlText stringByAppendingFormat:#"%#%#", _artistDetail.strContent, #"</div>"];
htmlText = [htmlText stringByReplacingOccurrencesOfString:#"''" withString:#"'"];
UIWebView *webView = [[UIWebView alloc]initWithFrame:frame];
[webView loadHTMLString:htmlText baseURL:nil];
webView.scrollView.bounces = NO;
[self addSubview:webView];
This is the set of codes that i put it in to my app.
But need to adjust the height of the web view according to the content size.
If you set webview.delegate = self you can add the UIWebViewDelegate protocol method webViewDidFinishLoad: and invoke a small bit of javascript to determine the height:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *output = [webview stringByEvaluatingJavaScriptFromString:#"document.body. scrollHeight;"];
NSLog(#"height: %#", output);
}
Another method would be to determine the webView's scrollView contentSize in this method, since that should fit the entire website by the time this method is called.
The only problem with this method, is that this method is called when the page finishes loading. Images are handled differently, which means that the webView might change sizes due to images that finish loading.
try -
float height= [iText sizeWithFont:[UIFont fontWithName:#"FontSpecifiedInTheHtmlText" size: -SpecifiedInHTMLText- constrainedToSize:CGSizeMake(320, 100000)lineBreakMode:UILineBreakModeWordWrap].height;
after u get the height of the content text, u can set the frame/bounds of the WebView.
You may want this:
[webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 20, 0)];
UIWebview is scrollable. So when you open a page in UIWebview u need not adjust the height according to the content of the page. You can see all the content by scrolling the UIWebView. You should check that user interaction is enabled for your webview.
Hi! I'm working with an UITableView, with UITableViewCell.
The UITableViewCell has inside an UIWebView (because i'm using entities, bold text, ecc...).
The webview is loaded with this code:
UIWebView *testoLabel2 = (UIWebView*)[cell viewWithTag:3];
NSString *html = [NSString stringWithFormat:#"<html><font size=2>%#</font></body></html>", sottotitolo];
testoLabel2.opaque = NO;
testoLabel2.backgroundColor = [UIColor clearColor];
[testoLabel2 loadHTMLString:html baseURL:nil];
What's the problem?
That when i need to reload the data (for example after the change of the deviceOrientation) using
[my_table reloadData];
all the webView blink!
For just a second, but they blink! And it's a very ugly effect!
Do you know why?
Thanks!
EDIT: also if i scroll down the tableview, when i scroll up the webview that before were loaded are white! But just for a moment, after they load the text!
Not sure exactly why but it's good practice to set all view backgrounds in cells to a solid solid color. The flash could be the cell rendering it's background and then the web view on top of it. If you set the webview background to your desired color and leave it opaque then this shouldn't happen. Good luck!