2014-03-12 91 views
0

我有一個UIWebView顯示來自網絡的文本和圖像。如何從UIWebView全屏顯示圖像?

我想要這樣做,當我點擊UIWebView中的圖像時,我想在包括方向的全屏中顯示它。

所以我用以下代碼進行了測試。

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 
    if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
     NSURL *URL = [request URL]; 

     NSData *data = [[NSData alloc] initWithContentsOfURL:URL]; 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]]; 

     GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init]; 
     vc.liftedImageView = imageView; 

     [self presentViewController:vc animated:YES completion:nil]; 

     return NO; 
    } 

    else 
    { 
     return YES; 
    } 
} 

它不起作用,並顯示錯誤,當我點擊圖像在UIWebView

2014-03-12 13:59:40.397 MMSP[1368:a0b] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 0; nan nan]' 
*** First throw call stack: 

有沒有辦法像我上面說的那樣做?

+0

我沒有設置imageView frame becz GGFullScreenImageViewController可以自動完成 –

+0

您確定該url包含圖片數據嗎? – sage444

回答

1

一)我想你應該一般設置GGFullscreenImageViewController

B)的幀(沒有你的崩潰做),我想你應該使用[[NSData的頁頭] initWithContentsOfURL:URL]。在這一點上加載圖像。點擊鏈接後,它會一直等到圖片下載完畢,然後顯示,導致用戶「滯後」。

相反,只需將網址移交給您的GGFullscreenImageViewController視圖控制器,並讓它顯示一個活動指示符,直到它已下載圖像。

+0

如何直接顯示GGFullScreenImageViewController的網址? –

+0

我的歉意,我認爲GGFullScreenImageViewController是你自己寫的一個類。既然不是這樣,你似乎無法像提到的那樣意識到它。您可以修改GitHub中的代碼以允許這樣做,或者您可以在shouldStartLoadWithRequest方法中顯示活動指示符(即創建一個新的半透明視圖並顯示它),並在完成時加載GGFullScreenImageViewController。 – cania

1

如果您的GGFullscreenImageViewController已經有UIImageView設置,那麼爲什麼不創建UIImage並分配,而不是創建一個新的UIImageView。當您分配新創建的實例時,liftedImageView中的圖像視圖實例會丟失。

只是這樣做,

NSData *data = [[NSData alloc] initWithContentsOfURL:URL]; 
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init]; 
vc.liftedImageView.image = [UIImage imageWithData:data]; 

理想情況下,你應該提供initWithUrl:方法傳遞圖像URL到GGFullscreenImageViewController。不同步地下載圖像或者你可以使用這樣做的開源代碼,例如:SDWebImage

希望這會有所幫助!