问题的总结

我是一个iPad应用程序,其中UIScrollView内有一个UIImageView。我希望用户能够通过使用手写笔和/或他们的手指在UIImageView上绘制。

我在模拟器上运行良好,但是在iPad上完全落后(有时会崩溃)。当您在UIScrollView上已经放大的情况下尝试干燥时,问题变得更加明显。它变得难以置信的松弛(本质上是冻结),然后崩溃。

(在UIScrollView上滚动不会与图形冲突,因为在 Activity 图形时将其设置为需要2个手指。)

代码

以下是相关方法。这些方法都在处理图形的视图内部:

 /**
 When the user first starts writing
 */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    isMouseMoved = NO;
    UITouch *touch = [touches anyObject];

    CGPoint currentPoint = [touch locationInView:imageView];

    if(isEraserOn){
        [self changeEraserLocationTo:currentPoint];
    }

    [self resetEraser:FALSE];
    lastPoint = [touch locationInView:imageView];
}

/**
 When the user first starts moving the pen
 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    isMouseMoved = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:imageView];

    // Setting up the context
    UIGraphicsBeginImageContext(imageView.frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

    if (isEraserOn) {
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), eraserRadius);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
        CGRect eraserFrame = eraser.frame;
        eraserFrame.origin.x = currentPoint.x - (eraserRadius/2);
        eraserFrame.origin.y = currentPoint.y - (eraserRadius/2);
        eraser.frame = eraserFrame;
    } else {
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), penRadius);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, 1.0);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
    }

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;
    mouseMoved++;

    if (mouseMoved == 1) {
        mouseMoved = 0;
    }
}

/**
 When the user stops writing
 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self resetEraser:TRUE];

    if (!isMouseMoved) {
        UIGraphicsBeginImageContext(imageView.frame.size);
        CGContextRef contextRef = UIGraphicsGetCurrentContext();

        [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

        CGContextSetLineWidth(contextRef, penRadius);
        CGContextSetRGBStrokeColor(contextRef, r, g, b, 1.0);
        CGContextMoveToPoint(contextRef, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(contextRef, lastPoint.x, lastPoint.y);
        CGContextStrokePath(contextRef);
        imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

对这个问题的任何帮助将受到的极大支持,表示赞赏;我真的很想用这个...

谢谢,
亚历克斯

最佳答案

我最近开发了一个应用程序,可以在图像上绘图。我实现它的方法是创建另一个UIView层,然后在该视图上绘制图形/ gl。然后,您可以将所有触摸事件移至gl视图。我当前的实现没有任何性能问题,放大时不应该更改它。还要检查内存,您可能正在泄漏。

看一下下面的苹果代码:

GLPaint

10-06 13:11
查看更多