iOS油漆优化

iOS油漆优化,ios,uiimage,cgcontext,Ios,Uiimage,Cgcontext,嗨,我目前正在开发一个应用程序,其中包含通过画画记笔记。我遵循ray wenderlich教程,就我所知,我最终得到了以下代码: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = NO; UITouch *touch = [touches anyObject]; lastPoint = [touch locationInView:self]; } - (v

嗨,我目前正在开发一个应用程序,其中包含通过画画记笔记。我遵循ray wenderlich教程,就我所知,我最终得到了以下代码:

- (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倍时,不幸的是性能不是很好。所以我在考虑优化代码。我特别关注触摸移动的方法。据我所知,它在上下文上绘制整个图像,并对其进行一些更改,然后将上下文指定给图像。绘制整个图像似乎是超负荷的。所以我想知道,我是否可以将图像的某些部分绘制到上下文中,并进行一些更改,然后将这部分上下文绘制到图像中

你说得对-每次在TouchsMoved中重新绘制整个图像都是个坏主意。我认为您应该在开始时创建并保留对上下文的引用。在moved中,您应该利用它并根据上下文创建图像。您可以使用
CGBitmapContextCreate()
而不是
UIGraphicsBeginImageContext()
来创建上下文,也可以使用
CGBitmapContextCreateImage()
从上下文而不是
UIGraphicsGetImageFromCurrentImageContext()
来创建图像。下面是关于如何使用这些()的文档