2014-01-07 46 views
0

我使用UIWebView從電子郵件中加載ppt文件,並使用renderInContext將ppt頁面轉換爲圖像:api在主線程的do while循環中。使用renderInContext呈現webview圖層時iOS應用崩潰:api

它工作正常,當我加載小於50張幻燈片小ppt文件。當我加載100頁PPT文檔時,我的應用崩潰了。崩潰發生在第83次renderInContext:API(從UIWebview呈現UILayer)。

下面是我在ARC應用程序中使用的一段代碼。

do 
    { 
     @autoreleasepool 
     { 
      NSLog(@"Image Index: %d",i); 
      UIGraphicsBeginImageContext(CGSizeMake(768.0f, 576.0f)); 
      CGContextRef ctxCurrentGraphics = UIGraphicsGetCurrentContext(); 

      [webView.layer renderInContext:ctxCurrentGraphics]; 
      CGContextSetInterpolationQuality(ctxCurrentGraphics, kCGInterpolationHigh); 
      CGContextSetRenderingIntent(ctxCurrentGraphics, kCGRenderingIntentDefault); 
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

      UIGraphicsEndImageContext(); 

      /******* Each snap shot been inserted to db before new snapshot created ********/ 

      if (image != nil) 
      { 
       [[DataManager instance] insertImportedImageToDb:image importFileName:filename ...]; 
       slideID++; 
       seqID++; 
      } 

      [webView.scrollView scrollRectToVisible:CGRectMake(0.0f, (i * 581.3) , webView.bounds.size.width, webView.bounds.size.height) animated:NO]; 
      scrolloffset = scrolloffset + 581.3; 

      i++; 

      /***** check if ppt contains only one page ****/ 
      if (webView.scrollView.contentSize.height == 1004.0 || webView.scrollView.contentSize.height == 1005.0) 
      { 
       break; 
      } 

      image = nil; 
     } 
    } 
    while (scrolloffset <= webView.scrollView.contentSize.height); 

該應用僅在設備(iOS6)上崩潰。我嘗試了所有替代方案,我發現堆棧溢出像清除上下文,使用@autoreleasepool等

任何人都可以幫助我有什麼辦法可以優化內存使用或有任何替代renderInContext:它看起來像API這個API有內存泄漏。

EDIT1:大多數時候,應用程序收到「內存不足警告」,但兩次我看到下面的崩潰日誌:

Last Exception Backtrace: 
0 CoreFoundation     0x316aa29e 0x315e8000 + 795294 
1 libobjc.A.dylib     0x3954e97a 0x39546000 + 35194 
2 CoreFoundation     0x316aa1c0 0x315e8000 + 795072 
3 UIKit       0x3389dd5a 0x334af000 + 4123994 
4 MyApplication     0x000c590c 0xa3000 + 141580 
5 MyApplication     0x000c4d26 0xa3000 + 138534 
6 Foundation      0x31fc1f4e 0x31f10000 + 728910 
7 CoreFoundation     0x3167f5da 0x315e8000 + 619994 
8 CoreFoundation     0x3167f28c 0x315e8000 + 619148 
9 CoreFoundation     0x3167defc 0x315e8000 + 614140 
10 CoreFoundation     0x315f0eb8 0x315e8000 + 36536 
11 CoreFoundation     0x315f0d44 0x315e8000 + 36164 
12 GraphicsServices    0x351a52e6 0x351a0000 + 21222 
13 UIKit       0x335062fc 0x334af000 + 357116 
14 MyApplication    0x000a8c60 0xa3000 + 23648 
15 libdyld.dylib     0x39985b1c 0x39984000 + 6940 

感謝, 西瓦

+0

它給了什麼錯誤? –

+0

更新我的問題與錯誤的詳細信息。大部分時間,應用都收到了「低內存警告」。 – Srivathsa

回答

0

問題的根本原因是遞歸地捕捉來自webview的圖像,其中renderInContext:用於捕獲。

這個問題只發生在83次圖像捕捉(for循環中的第84次迭代)之後,因此我感覺存在巨大的內存泄漏。

不確定這是對這個問題的樂觀解決方案。但我設法解決使用下面的步驟..

(1)renderInContext:不立即釋放圖形上下文,因此積累內存,因此低內存警告。我試過的解決方案是將整個功能保留在@autorelease下,並延遲捕獲下一個1毫秒的圖像,這在性能方面可以忽略不計。

(2)避免使用forloop並將圖像存儲在可變數組中。這也節省了一些記憶。這也減少了堆內存使用量。

希望這有助於有人..

0

我想原因是API:renderInContext不會立即導致的低內存warnings.When內存不足警告的多次出現,系統將終止你的程序釋放圖形上下文,而不是崩潰。

我設法使用下面的步驟來解決:

//render 
[selectView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

//clear the cache 
//UIWebview 
if ([selectView isKindOfClass:[UIWebView class]]) 
{ 
    [(UIWebView *)selectView loadHTMLString:nil baseURL:nil]; 
//others 
}else 
{ 
    selectView.layer.contents = (id)nil; 
} 

Hope this helps somebody..