2013-07-18 57 views
2

嗨我目前正在研究一個應用程序,其中包含通過繪圖筆記。我跟着射線wenderlich教程而據我已經得到了,我結束了這段代碼:iOS油漆優化

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

    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 
    lastPoint = [touch locationInView:self]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGFloat red,green,blue,alpha; 

    mouseSwiped = YES; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 



    UIGraphicsBeginImageContext(self.mainImage.frame.size); 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)]; 

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),[self getBlendMode]); 

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x , lastPoint.y ); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x , currentPoint.y ); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), [self getBrushSize]); 

    [[self getPaintColor] getRed:&red green:&green blue:&blue alpha:&alpha]; 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1); 

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),[self getBlendMode]); 

    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.tempDrawImage setAlpha:[self getPaintAlpha]]; 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGFloat red,green,blue; 

    if(!mouseSwiped) { 

     UIGraphicsBeginImageContext(self.mainImage.frame.size); 
     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), [self getBrushSize]); 

     [[self getPaintColor] getRed:&red green:&green blue:&blue alpha:nil]; 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, [self getPaintAlpha]); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y - self.mainImage.frame.origin.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y - self.mainImage.frame.origin.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

    UIGraphicsBeginImageContext(self.mainImage.frame.size); 

    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height) blendMode:[self getBlendMode] alpha:1.0]; 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height) blendMode:[self getBlendMode] alpha:[self getPaintAlpha]]; 
    if(self.drawMode != DrawEraser) 
    { 
     self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     self.tempDrawImage.image = nil; 
    } 
    UIGraphicsEndImageContext(); 
    mouseSwiped = NO; 
} 

該代碼是工作只是一個小框罰款,但是當我增加了幀比以前大2倍,不幸的是,表現並不好。所以我在考慮優化代碼。我特別專注於touchesMoved方法。據我瞭解,它會在上下文中繪製整個圖像,並對其進行更改並將上下文分配給圖像。繪製整個圖像似乎是超負荷的。所以我想知道,如果我可以將圖像的某些部分繪製到上下文中並進行一些更改,然後將這部分上下文繪製到圖像上。

回答

1

你是對的 - 每次在touchesMoved重畫整個圖像是一個壞主意。我認爲你應該在開始時創建並保持對上下文的引用。在動人的情況下,你應該借鑑並從上下文創建一個圖像。您可以使用CGBitmapContextCreate()來創建上下文,而不是使用UIGraphicsBeginImageContext()CGBitmapContextCreateImage()來創建上下文而不是UIGraphicsGetImageFromCurrentImageContext()的圖像。以下是有關如何使用這些文件的文件(https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGBitmapContext/Reference/reference.html)。

+0

謝謝,我會看看文檔 – mayy00