【问题标题】:Using Core Graphics for drawing使用 Core Graphics 进行绘图
【发布时间】:2012-05-15 18:59:54
【问题描述】:

我正在构建一个 iOS 应用程序,用于第一次绘图。现在我可以使用核心图形绘制直线和曲线,但无法撤消绘图。我在绘制时保存了所有点,并尝试将它们重用于 UNDO 和 REDO,但没有成功。谁能帮忙知道我做错了什么?

非常感谢任何帮助。

这是我的代码

   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [tempPathArray removeAllObjects];
    UITouch *touch = [touches anyObject];

    previousPoint1 = [touch previousLocationInView:self];
    previousPoint2 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

}

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch  = [touches anyObject];

    previousPoint2  = previousPoint1;
    previousPoint1  = [touch previousLocationInView:self];
    currentPoint    = [touch locationInView:self];


    // calculate mid point
    CGPoint mid1    = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2    = midPoint(currentPoint, previousPoint1);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGRect bounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);

    drawBox = bounds;

    //Pad our values so the bounding box respects our line width
    drawBox.origin.x        -= self.lineWidth * 2;
    drawBox.origin.y        -= self.lineWidth * 2;
    drawBox.size.width      += self.lineWidth * 4;
    drawBox.size.height     += self.lineWidth * 4;

    UIGraphicsBeginImageContext(drawBox.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    curImage = UIGraphicsGetImageFromCurrentImageContext();
    [curImage retain];
    UIGraphicsEndImageContext();

    [tempPathArray addObject:[NSValue valueWithCGRect:drawBox]];


    [self setNeedsDisplayInRect:drawBox];

}

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    [pathArray addObject:tempPathArray];
    NSLog(@"path array count %d", [pathArray count]);
}

    - (void)drawRect:(CGRect)rect
{
    NSLog(@"draw rect");
    [curImage drawAtPoint:CGPointMake(0, 0)];
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    context = UIGraphicsGetCurrentContext(); 

    [self.layer renderInContext:context];

    CGContextMoveToPoint(context, mid1.x, mid1.y);
    // Use QuadCurve is the key
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    CGContextStrokePath(context);
    [super drawRect:rect];

}

    -(void)undoButtonClicked
{

    KidsPhotoBookAppDelegate *appDelegate = (KidsPhotoBookAppDelegate*)[[UIApplication sharedApplication]delegate];


    if([pathArray count]>0){
        [bufferArray addObject:[pathArray lastObject]];
        [pathArray removeLastObject];

        for (NSMutableArray *tempArray in pathArray) {
            for (int i  = 0; i < [tempArray count]; i++) {
                CGRect draw = [[tempArray objectAtIndex:i] CGRectValue];
                [self setNeedsDisplayInRect:draw];
            }
        }
    }


}

【问题讨论】:

    标签: iphone ios drawing core-graphics


    【解决方案1】:

    试试这个:

    1. 画一条线。
    2. 在第一条线上再画一条线。
    3. 撤消。

    我猜你会发现它大部分都有效——第二行与第一行重叠的部分会消失,只有不重叠的部分会保留。

    问题是您设置的需要显示在您仍然拥有的绘图部分上,而不是您刚刚腾出的部分上。这与您应该做的正好相反:您仍然拥有的部分不需要重新绘制,因为它们没有改变,而您腾出的部分确实需要重新绘制,因为它已经改变了。

    因此,设置需要显示在您要删除的路径区域中,就在您删除它之前。您不需要设置其他任何内容的需求显示。

    【讨论】:

    • 其实我无法理解你的理论。恐怕我达不到你的理解水平。请问你能进一步解释一下吗?
    • @GhazanfarAli:就像我说的,你正在设置需要显示在你仍然拥有的部分上,但那些不需要重新绘制。您需要重绘的区域是您刚刚删除路径的区域。因此,set 只需要显示在您要删除的路径区域,而不是您要保留的路径区域。
    • @AbhishekBedi:你想通过链接到那个问题说什么?
    • @PeterHosey:上面链接中人们之间的对话解决了这个问题。没有像 CGRemovePathFromContext 这样的方法。参考类似问题:[lists.apple.com/archives/quartz-dev/2007/May/msg00064.html]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多