Objective c 在大型图像上绘制CoreGraphics

Objective c 在大型图像上绘制CoreGraphics,objective-c,ios,cocoa-touch,core-graphics,Objective C,Ios,Cocoa Touch,Core Graphics,我有一个UIImage,我在上面画了一条线,沿着用户的手指。它就像一块画板。当UIImage很小(比如500 x 600)时,这一点非常有效,但是如果它像1600 x 1200,它会变得非常粗糙和滞后。有什么方法可以优化它吗?这是我在touchesModed中的绘图代码: UIGraphicsBeginImageContext(self.frame.size); [drawImageView.image drawInRect:CGRectMake(0, 0, self.frame.size.wi

我有一个UIImage,我在上面画了一条线,沿着用户的手指。它就像一块画板。当UIImage很小(比如500 x 600)时,这一点非常有效,但是如果它像1600 x 1200,它会变得非常粗糙和滞后。有什么方法可以优化它吗?这是我在touchesModed中的绘图代码:

UIGraphicsBeginImageContext(self.frame.size);
[drawImageView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
//CGContextSetAlpha(UIGraphicsGetCurrentContext(), 0.5f);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

谢谢。

与其一次性绘制整个1600X1200帧,不如根据需要绘制。我的意思是,由于您正在绘制整个框架(驻留在内存中),因此您的性能参差不齐

试试看。基本上,你只需要画一个你的设备能够显示的屏幕大小,当用户滚动时,你需要画一个屏幕大小


这是在iPad或iPhone上的谷歌地图中使用的。希望这有助于…

不要在每次触摸移动时创建新的上下文并将当前图像绘制到其中,而是使用
CGBitmapContextCreate
创建上下文并重用该上下文。所有以前的图形都已在上下文中,并且您不必在每次触摸移动时创建新的上下文

- (CGContextRef)drawingContext {
    if(!context) { // context is an instance variable of type CGContextRef
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        if(!colorSpace) return nil;
        context = CGBitmapContextCreate(NULL, contextSize.width, contextSize.height,
                                        8, contextSize.width * 32, colorSpace,
                                        kCGImageAlphaPremultipliedFirst);
        CGColorSpaceRelease(colorSpace);
        if(!context) return nil;
        CGContextConcatCTM(context, CGAffineTransformMake(1,0,0,-1,0,contextSize.height));
        CGContextDrawImage(context, (CGRect){CGPointZero,contextSize}, drawImageView.image.CGImage);
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    }
    return context;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGContextRef ctxt = [self drawingContext];
    CGContextBeginPath(ctxt);
    CGContextMoveToPoint(ctxt, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(ctxt, currentPoint.x, currentPoint.y);
    CGContextStrokePath(ctxt);
    CGImageRef img = CGBitmapContextCreateImage(ctxt);
    drawImageView.image = [UIImage imageWithCGImage:img];
    CGImageRelease(img);
}
此代码需要实例变量
CGContextRef context
CGSize contextSize
。上下文将需要在dealloc中发布,并且无论何时更改其大小