2011-08-08 34 views
0

我有一个UIImage,我在它上面画用户手指的线条。它就像一个绘图板。当UIImage很小时,比如说500 x 600,这个效果非常好,但是如果它像1600 x 1200一样,它会变得非常粗糙和不稳定。有什么方法可以优化这个吗?这是我在绘图中的代码编号:CoreGraphics在大型UIImage上绘图

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(); 

谢谢。

回答

0

而不是一次绘制整个1600X1200框架,为什么不画出它所需要的。我的意思是,因为你正在绘制整个框架(驻留在内存中),所以你的表现很糟糕。

尝试CATiledLayer。基本上你需要绘制一个只有你的设备能够支持的屏幕尺寸,除此之外,当用户滚动时,你需要动态绘制它。

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

0

每次触摸移动时,都不会创建新的上下文并绘制当前图像,而是使用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 contextCGSize contextSize。上下文将需要在dealloc中发布,并且每当您更改其大小时。