繁体   English   中英

UIWebView更改方向时更改图像

[英]UIWebView change image when orientation changes

我想当方向改变时,我的uiwebview会改变gif图像,因此最初我正在使用以下代码加载第一个图像:

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];

我将以下代码放在directionChanged函数中:

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];

所以图像在第一次显示时工作正常,当我旋转设备时,出现了(横向或纵向)图像,但是图像的一部分被截断了。(每次旋转后,图像的一部分将被忽略.....),任何理念?

感谢帮助

那是因为当您按照xib文件的指示加载nib文件时,uiimageview很可能放在最后,所以我猜您需要修改帧坐标才能显示who图像。 如果要测试该理论,请尝试在图像末尾的xib文件中放置一个按钮,然后运行该应用程序并旋转设备以lanscape显示,如果我的理论正确,则该按钮将不会显示。 我希望这有帮助! 祝好运!

我想这全都是关于编排问题。 在这里你应该做的事情

1)每当取向变化到达该点并识别出取向时,假设您已获得肖像,则调用您的方法,该方法进行将肖像设置为新图像的设置,反之亦然。

这是我的逻辑,请仔细看。

下面的方法在每次定向发生变化时都会调用。这里,您只需要允许定向发生变化即可,因为您还需要将图像设置为横向。

 - (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.

}

注意:此处我仅针对您当前的需求发布此答案,我不知道您如何管理方向...

希望对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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