Objective c 如何在objective c中绘制大量的线条和形状?
在过去的5个月里,我一直在使用objective c。所以现在我想 喜欢制作一个用户能够通过交互绘制大量线条的应用程序(例如手指滑动) 所以我开始使用drawrect方法和核心图形库。因此,我制作了一个数组来保存CGPoints,在调用[self-setNeedsDisplay]时,应用程序从一开始就重新绘制了整个屏幕(对于drawrect中的循环,因为我需要在触摸中移动手指时绘制和擦除线条的能力)。例如,当阵列有超过1000行时,就会出现问题,因为响应速度太慢 除了寻求更好的性能外,我还提出了一些方法,在视图中绘制每条线,然后添加此视图(使用CGBitmapContextCreate、uiimageview),(对于TouchsMoved,appi每次都删除上一个视图并添加新视图) 不幸的是,结果是一样的。在画布上添加越来越多的上下文(线条、形状等)时,性能非常差 如果你能回答这个问题,我将不胜感激。 哪种技术最适合在画布上绘制大量线条?Cocos 2d是一种选择吗?OpenGL是我最后的选择,我更喜欢coreGraphics库附近的解决方案。是否有一种方法可以使用核心图形在绘制第一行和第1000行时具有相同的性能 对不起,我的英语( 更新日期:2012年6月15日 下面是代码。我使用了Dimitris在这篇文章中的一些想法: 我还必须说,我有一个方法(在outArray中添加cgpoints),它在ToucheSBegind、TouchesMoved和TouchesEnded中运行 因此,我尝试在手指移动期间画一条线(在移动期间也会删除前一条线),但屏幕上的基本线(或前一条结束线)消失了。我还尝试在手指移动期间添加一个子视图(在此模型中),同时删除前一个子视图,但也会增加应用程序中的延迟 到目前为止,绘图的情况对我来说太混乱了。在android中,是一个绘制(onDraw)的方法,该方法更新视图,并且不覆盖视图(使用invalidate()调用)。 你有什么建议吗?我必须重复一遍,我想画出性能良好的第1200条线,没有(光学)延迟。Objective c 如何在objective c中绘制大量的线条和形状?,objective-c,performance,drawing,lines,Objective C,Performance,Drawing,Lines,在过去的5个月里,我一直在使用objective c。所以现在我想 喜欢制作一个用户能够通过交互绘制大量线条的应用程序(例如手指滑动) 所以我开始使用drawrect方法和核心图形库。因此,我制作了一个数组来保存CGPoints,在调用[self-setNeedsDisplay]时,应用程序从一开始就重新绘制了整个屏幕(对于drawrect中的循环,因为我需要在触摸中移动手指时绘制和擦除线条的能力)。例如,当阵列有超过1000行时,就会出现问题,因为响应速度太慢 除了寻求更好的性能外,我还提出了
thanx您应该做的第一个优化是只重新绘制需要更新的区域。而不是在
touchsmoved:withEvent:
中的setNeedsDisplay
,计算您需要更新的区域(基本上是一个包含最后两个触摸位置的矩形),并使用此区域调用setNeedsDisplayInRect:
然后,在
drawRect:
中的for
循环中,确定任何给定形状是否属于此矩形,并仅在它属于此矩形时绘制它。你好,奥勒,非常感谢您的回答。我再次尝试了上述方法。thanx。我期待着您的反馈。:)
- (BOOL) initContext:(CGSize)size {
int bitmapByteCount;
int bitmapBytesPerRow;
bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);
cacheBitmap = malloc( bitmapByteCount );
cacheBitmapSwiped= malloc( bitmapByteCount );
ctx = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
//for finger sweaping
cacheContextSwiped = CGBitmapContextCreate (cacheBitmapSwiped, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
return YES;
}
//method which draw a line on TouchesEND action
- (void) drawLine:(id)sender {
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
CGContextSetStrokeColorWithColor(ctx, [color CGColor]);
CGContextSetLineWidth(ctx, 4);
//outArray:array which holds cgpoints
if ([outArray count] > 0)
{
CGPoint point1 = [(NSValue *)[[outArray objectAtIndex:[outArray count] -1 ] objectAtIndex: 0] CGPointValue];
CGPoint point2 = [(NSValue *)[[outArray objectAtIndex:[outArray count] -1 ] objectAtIndex: 1] CGPointValue];
///ADDON
CGRect rectPoint1 = CGRectMake(point1.x-10, point1.y-10, 20, 20);
CGRect rectPoint2 = CGRectMake(point2.x-10, point2.y-10, 20, 20);
CGRect newRect = CGRectUnion(rectPoint1, rectPoint2);
///ADD NEW CHECK
if (CGRectContainsPoint(newRect, point1) && CGRectContainsPoint(newRect, point2)) {
CGContextMoveToPoint(ctx, point1.x, point1.y);
CGContextAddLineToPoint(ctx, point2.x, point2.y);
CGContextStrokePath(ctx);
}
[self setNeedsDisplayInRect:newRect];
}
}
//method which draws lines on TouchesMoved action
//draw in rect
-(void)drawLineSwiped:(id)sender{
//size of main image RECT
CGSize mainImageRectSize = mainImageRect.size;
int bitmapByteCount;
int bitmapBytesPerRow;
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (mainImageRectSize.width * 4);
bitmapByteCount = (bitmapBytesPerRow * mainImageRectSize.height);
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
//bitmap data
//bitmap data for finger sweaped
cacheBitmapSwiped= malloc( bitmapByteCount );
//for finger sweaping
cacheContextSwiped = CGBitmapContextCreate (cacheBitmapSwiped, mainImageRectSize.width, mainImageRectSize.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
UIColor *color = [UIColor colorWithRed:0.0 green:0 blue:1.0 alpha:1.0];
CGContextSetStrokeColorWithColor(cacheContextSwiped, [color CGColor]);
CGContextSetLineWidth(cacheContextSwiped, 2);
if ([outArray count] > 0)
{
CGPoint point1 = [(NSValue *)[[outArray objectAtIndex:[outArray count] -1 ] objectAtIndex: 0] CGPointValue];
CGPoint point2 = [(NSValue *)[[outArray objectAtIndex:[outArray count] -1 ] objectAtIndex: 1] CGPointValue];
CGRect rectPoint1 = CGRectMake(point1.x-10, point1.y-10, 20, 20);
CGRect rectPoint2 = CGRectMake(point2.x-10, point2.y-10, 20, 20);
CGRect newRect = CGRectUnion(rectPoint1, rectPoint2);
if (CGRectContainsPoint(newRect, lastPoint1) && CGRectContainsPoint(newRect, newPoint)) {
//draw line into the screen
CGContextMoveToPoint(cacheContextSwiped, point1.x, point1.y);
CGContextAddLineToPoint(cacheContextSwiped, point2.x, point2.y);
CGContextStrokePath(cacheContextSwiped);
}
free(cacheBitmapSwiped);
[self setNeedsDisplayInRect:newRect];
}
}
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
if (mouseEnded) {
CGImageRef cacheImage;
cacheImage= CGBitmapContextCreateImage(ctx);
CGContextDrawImage(context, self.bounds, cacheImage);
CGImageRelease(cacheImage);
}
else if(mouseSwiped ){
CGImageRef cacheImageSwiped;
cacheImageSwiped= CGBitmapContextCreateImage(cacheContextSwiped);
CGContextDrawImage(context, self.bounds, cacheImageSwiped);
CGImageRelease(cacheImageSwiped);
}