2012-06-20 24 views
2

我有一個非常大的UIView大小約3000x3000。在這個大視圖中,我使用觸筆或手指在iPad上做了一些自由形式的繪圖。這裏是我的觸摸代碼開始並移動方法。setNeedsDisplay在一個大的UIView導致

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{  

     Line_Point * to_be_added = [[Line_Point alloc] init]; 
     to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor]; 
     to_be_added.line_pattern = [NSString stringWithString: current_line_pattern]; 

     UITouch *touch = [[event touchesForView:self] anyObject]; 

     CGPoint location = [touch locationInView:self]; 
     to_be_added.point = [NSValue valueWithCGPoint:(location)]; 

     if (self.points == nil) 
     { 
      NSMutableArray *newPoints = [[NSMutableArray alloc] init]; 
      self.points = newPoints; 
      [newPoints release]; 
     } 

     [self.points addObject:to_be_added];   



} 

這裏是我的觸摸移動方法,這似乎是導致該問題,因爲它不是獲取調用足夠的,因爲應用程序調用setNeedsDisplay現在

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

     Line_Point * to_be_added = [[Line_Point alloc] init]; 
     to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor]; 
     to_be_added.line_pattern = [NSString stringWithString: current_line_pattern]; 

     UITouch * touch = [[event touchesForView:self] anyObject]; 
     CGPoint location = [touch locationInView:self]; 
     to_be_added.point = [NSValue valueWithCGPoint:(location)]; 

     [self.points addObject:to_be_added];   


     [self setNeedsLayout]; 

} 

這裏是顯示繪圖

我的drawRect方法
-(void)drawRect:(CGRect)rect 
{ 
if (self.points.count == 0)   
    { 

    } 
    else 
    {  
     Line_Point * first = [self.points objectAtIndex:0]; 
     CGContextRef context2 = UIGraphicsGetCurrentContext(); 
     CGContextSetStrokeColorWithColor(context2, first.line_color.CGColor); 
     if ([first.line_pattern isEqualToString:@"normal"]) 
     { 
      CGContextSetLineWidth(context2, 4.0); 
      CGContextSetLineDash(context2, 0, NULL, 0); 
     } 
     else if([first.line_pattern isEqualToString:@"thick"])    
     { 
      CGContextSetLineWidth(context2, 6.0); 
      CGContextSetLineDash(context2, 0, NULL, 0); 
     } 
     else if([first.line_pattern isEqualToString:@"dotted"])    
     { 
      CGFloat Pattern[] = {5, 5, 5, 5}; 
      CGContextSetLineDash(context2, 0, Pattern, 4); 
     } 
     else if([first.line_pattern isEqualToString:@"super_dotted"])    
     { 
      CGFloat Pattern[] = {1, 2, 1, 2}; 
      CGContextSetLineDash(context2, 0, Pattern, 4); 
     }   
     CGPoint firstPoint2 = [first.point CGPointValue]; 
     CGContextBeginPath(context2); 
     CGContextMoveToPoint(context2, firstPoint2.x, firstPoint2.y); 

     int i2 = 1; 
     while (i2 < self.points.count) 
     { 
      Line_Point * nextPoint = [self.points objectAtIndex:i2]; 

      if (nextPoint.point.CGPointValue.x < 0 && nextPoint.point.CGPointValue.y < 0) 
      { 
       CGContextDrawPath(context2, kCGPathStroke); 

       if (i2 < (self.points.count-1)) 
       { 

        CGContextBeginPath(context2); 
        Line_Point * nextPoint2 = [self.points objectAtIndex:i2+1];     
        CGContextMoveToPoint(context2, nextPoint2.point.CGPointValue.x, nextPoint2.point.CGPointValue.y); 
        CGContextSetStrokeColorWithColor(context2, nextPoint2.line_color.CGColor); 
        if ([nextPoint2.line_pattern isEqualToString:@"normal"]) 
        { 
         CGContextSetLineWidth(context2, 4.0); 
         CGContextSetLineDash(context2, 0, NULL, 0); 
        } 
        else if([nextPoint2.line_pattern isEqualToString:@"thick"])    
        { 
         CGContextSetLineWidth(context2, 6.0); 
         CGContextSetLineDash(context2, 0, NULL, 0); 
        } 
        else if([nextPoint2.line_pattern isEqualToString:@"dotted"])    
        { 
         CGFloat Pattern[] = {5, 5, 5, 5}; 
         CGContextSetLineDash(context2, 0, Pattern, 4); 
        } 
        else if([nextPoint2.line_pattern isEqualToString:@"super_dotted"])    
        { 
         CGFloat Pattern[] = {1, 2, 1, 2}; 
         CGContextSetLineDash(context2, 0, Pattern, 4); 
        }   
        i2 = i2 + 2; 

       } 
       else 
        i2++; 
      } 
      else 
      { 
       CGContextAddLineToPoint(context2, nextPoint.point.CGPointValue.x, nextPoint.point.CGPointValue.y); 
       i2++; 
      } 
     } 

     CGContextDrawPath(context2, kCGPathStroke); 
    } 

當在自由格式模式下繪製時,應用程序令人難以置信地滯後。我相信這是因爲視圖太大,並且setNeedsDisplay在每次調用觸摸移動方法時重新加載整個視圖。有沒有辦法加快setNeedsDisplay方法,使自由格式繪圖不會滯後於iPad。我可以只針對發生繪圖的上下文不重新加載整個事情嗎?當我將尺寸調整到大約1000x1000時,它沒有滯後問題。

感謝您對此事的任何幫助。

EDIT TRYED USING -SetNeedsDisplayinRect

UIView的纏繞一個UIScrollView內部和這裏是代碼。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 

    Line_Point * to_be_added = [[Line_Point alloc] init]; 
    to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor]; 
    to_be_added.line_pattern = [NSString stringWithString: current_line_pattern]; 

    UITouch * touch = [[event touchesForView:self] anyObject]; 
    CGPoint location = [touch locationInView:self]; 
    to_be_added.point = [NSValue valueWithCGPoint:(location)]; 

    [self.points addObject:to_be_added];   


      CGRect visibleRect; 
     visibleRect.origin = big_view.scrollable_view.contentOffset; 
     visibleRect.size = CGSizeMake(950, 500);    
     [self setNeedsDisplayInRect:visibleRect]; 

} 

這種方法也沒有幫助。

如果可以幫忙,再次感謝。

回答

2

可以使用

- (void)setNeedsDisplayInRect:(CGRect)invalidRect 

,而不是-setNeedsDisplay

另一種解決方案(如果有過多的點,應用滯後因爲畫這些的) - 而不是在每一次渲染的所有點 - drawInRect被調用。例如,您可以每隔10次調用一次,從當前上下文保存圖像,然後僅顯示圖像。

更新:

嘗試添加

+(Class)layerClass 
{ 
return [CATiledLayer class]; 
} 

到您的視圖,滾動型

+0

我使用了上述方法,它似乎沒有工作。矩形沒有用。我甚至確定矩形是正確的。將編輯上面的代碼。 – Krzemienski

+0

我發現這個[鏈接](https://discussions.apple.com/thread/1630986?start=0&tstart=0)和其他一些,他們說在scrollView的大視圖問題。我會更新回答 – medvedNick

+0

AWSOME!有效! – Krzemienski

0

只是爲了說清楚,一個3000x3000pixel的UIView有一個CALayer的背後(它不一樣,如果平鋪的問題很多或不),它使用3000x3000x4字節的空間,所以大約34-35MB。您將不會在應用程序本身中看到內存使用情況,因爲它是在跳板中分配的。

從我的角度來看,最好的解決方案是平鋪圖層的圖層比視圖大一點(因爲它們可以渲染線程),然後啓用剪裁,併爲您的路徑分配適當的偏移到圖層。這是相當多的工作要做,但工作會很好。

啊,有時候,你要重做它,它有助於清除CALayers緩存。一個layer.contents =零會做到這一點....

乾杯,Jörg

相關問題