簡體   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