1

我在过去 5 个月使用目标 c。所以现在我想制作一个用户能够通过交互(例如手指滑动)绘制大量线条的应用程序。

所以我开始使用drawrect方法和Core图形库。因此,我创建了一个包含 CGPoints 的数组,并在调用 [self setNeedsDisplay] 时,appi 从一开始就重新绘制了整个屏幕(drawrect 中的 for 循环,因为我需要在 TouchesMoved 中能够在手指移动期间绘制和擦除线条)。例如,当阵列超过 1000 行时,就会出现问题,因为响应速度很慢。

除了寻求更好的性能外,我还做了一些方法来绘制视图中的每一行,然后添加这个视图(使用 CGBitmapContextCreate、uiimageview),(对于 TouchesMOved,appi 每次都删除前一个视图并添加新视图)。

不幸的是,结果是一样的。在画布上添加越来越多的上下文(线条、形状等)时,性能很差。

如果您能回答这个问题,我将不胜感激。哪种技术最适合在画布上绘制大量线条?Cocos 2d 是一个选项?Open gl 是我最后的选择,我更喜欢 coreGraphics 库附近的解决方案。有没有办法使用核心图形在绘制第一行和绘制第 1000 行时具有相同的性能?

对不起我的英语不好..:(

下面更新 15/06/12 是代码。我从这篇文章中使用了 Dimitris 的一些想法:

在 UIView (iPhone) 中增量绘图

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

另外我不得不说我有一个在 TouchesBegan、TouchesMoved、TouchesEnded 中运行的方法(在 outArray 中添加 cgpoints)。

所以我试图在手指移动期间画一条线(在移动过程中也删除了前一条线),但底层(或以前的 -end- 线)从我的屏幕上消失了。我还尝试在手指移动期间添加一个子视图(在此模型中),同时删除以前的子视图,但也增加了应用程序的延迟。

到现在为止,绘画的情况对我来说太混乱了。在 android 中,这是一个绘制 (onDraw) 的方法,此方法更新视图并且不会覆盖它(使用 invalidate() 调用)。你有什么建议吗?我必须重复一遍,我想在没有(光学)延迟的情况下以良好的性能绘制第 1200 条线。谢谢

4

1 回答 1

0

您应该做的第一个优化是只重绘需要更新的区域。而不是setNeedsDisplayin touchesMoved:withEvent:,计算您需要更新的区域(基本上是一个包含最后两个触摸位置的矩形)并setNeedsDisplayInRect:使用该区域进行调用。

然后,在您的for循环中drawRect:,确定任何给定的形状是否落入此矩形,如果是,则仅绘制它。

于 2012-06-14T09:59:01.763 回答