简体   繁体   中英

UIWebView change image when orientation changes

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 ur managing the Orientation ...

I hope it may helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM