2013-09-29 25 views
1

我正在寫一個應用程序,用戶可以在uiview上繪製。如果uiview的尺寸正常(比如說1024 x 720),它會很完美。但是,如果我將其添加到uiscrollview並且該維度爲1024 x 3000,則它變得非常緩慢。而且,如果高度爲10000,則應用程序會當場崩潰。我想知道該怎麼做。非常大的uiview和內存中的drawrect是不夠的

- (void) drawRect: (CGRect) rect 
{ 
    NSLog(@"drawrect here 1"); 

if (self.arrayStrokes) 
{ 
    int arraynum = 0; 
    // each iteration draw a stroke 
    // line segments within a single stroke (path) has the same color and line width 
    for (NSDictionary *dictStroke in self.arrayStrokes) 
    { 
     NSArray *arrayPointsInstroke = [dictStroke objectForKey:@"points"]; 
     UIColor *color = [dictStroke objectForKey:@"color"]; 
     float size = [[dictStroke objectForKey:@"size"] floatValue]; 
     [color set];  // equivalent to both setFill and setStroke 

     //   // won't draw a line which is too short 
     //   if (arrayPointsInstroke.count < 3) { 
     //    arraynum++; 
     //    continue;  // if continue is executed, the program jumps to the next dictStroke 
     //   } 

     // draw the stroke, line by line, with rounded joints 
     UIBezierPath* pathLines = [UIBezierPath bezierPath]; 
     CGPoint pointStart = CGPointFromString([arrayPointsInstroke objectAtIndex:0]); 
     [pathLines moveToPoint:pointStart]; 
     for (int i = 0; i < (arrayPointsInstroke.count - 1); i++) 
     { 
      CGPoint pointNext = CGPointFromString([arrayPointsInstroke objectAtIndex:i+1]); 
      [pathLines addLineToPoint:pointNext]; 
     } 
     pathLines.lineWidth = size; 
     pathLines.lineJoinStyle = kCGLineJoinRound; 
     pathLines.lineCapStyle = kCGLineCapRound; 
     [pathLines stroke]; 

     arraynum++; 
    } 
} 
} 

回答

1

代碼示例中沒有任何明顯的提示,會導致真正大視圖的問題。我做了一個快速測試,並繪製了1024 x 10,000的視圖,沒有發生任何事件。我跑的這個翻譯通過儀器,結果是令人滿意的平常:

Allocations

您應該通過儀器運行應用程序,(一)確保你有沒有泄漏;和(b)看看分配情況並確保它的水平。如果它在你身上增長,你應該確定是什麼導致了這種增長(通過選項 - 在分配窗口中進行縮小或進行堆積)。大量的問題可能會導致分配增加(無法使用某些核心基礎對象,上下文的開始/結束不匹配等),所以我毫不猶豫地猜測您的問題可能是什麼。我沒有看到您提供的代碼示例中的任何明顯內容。我只建議通過靜態分析器運行你的代碼(「產品」菜單上的「分析」),看看它是否能識別任何東西。

現在,有什麼問題可以使用renderInContextUIGraphicsGetImageFromCurrentImageContext()作爲UIImage保存。你還沒有說過你這樣做,但試圖創建/保存圖像將消耗這些大小的驚人數量的內存。

如果您不熟悉使用儀器來追蹤內存問題,我建議您觀看WWDC 2012視頻iOS App Performance: Memory。這是一個很好的主題。

+0

是。這是我的結果。 http://i40.tinypic.com/sbkidc.png。如果可能的話,你可以檢查一下這個代碼嗎? http://www.mediafire.com/download/i8jxi71it8p87tu/smoothline.zip。我在uiwebview的scrollview上使用這個uiview。我不確定是什麼導致應用程序變慢和崩潰。 –

+0

這是我在uiwebview scrollview中繪製的完整源代碼。我收到內存警告。 http://www.mediafire.com/?jg7d6a5q2dsai4s –

+0

@KhantThuLinn我們應該把這個聊天:http://chat.stackoverflow.com/rooms/38356/drawrect-in-very-large-uiview-and-memory-是不夠的 – Rob

0

如果手機犯規讓你創建的CGRect比看到一隻大的原因,如果手機運行內存不足there's幾個你可以做,嘗試用不同的解決方案接近你的問題,但手機的內存是有限的,你不能濫用。