在iOS 7中绘图时出现延迟

3

我有一个应用程序,在其中在一个视图上进行一些素描。迄今为止,它一直正常工作,直到我安装了ios7。我的应用程序使用触摸移动方法来识别移动的变化。但是当我画线时,触摸方法被调用,但是直到我在ios7中触摸结束后,线条才得到更新。因此绘图时存在轻微的延迟。它在ios6上和ios7模拟器上运行良好,但是当我在真实的ios7设备上测试它时,绘图算法存在延迟。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isImageSelection) {

            mouseSwiped = NO;
            UITouch *touch = [touches anyObject];

            if ( [touch view] != baseview) {

                lastPoint2 = [touch locationInView:self.viewDrawing2];
            }
        }
    }

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

          //  NSLog(@"in image selection==%d touch moved   ",isImageSelection );

            mouseSwiped = YES;

            UITouch *touch = [touches anyObject];
            // if (([touch view] != btnInkColor) || ([touch view] != btnPenSize)  || ([touch view] != baseview)) {
            if ( [touch view] != baseview) {

                CGPoint currentPoint = [touch locationInView:self.viewDrawing];



                if(isEraser) {
                    // [[NSUserDefaults standardUserDefaults] floatForKey:@"strokeValue"];
                    UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
                    [imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
                    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
                    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), stroke);
                    //uncommented by prerak
                        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);

                    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
                    CGContextBeginPath(UIGraphicsGetCurrentContext());
                    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
                    CGContextStrokePath(UIGraphicsGetCurrentContext());
                    imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                } else {
                    float strokeT= [[NSUserDefaults standardUserDefaults] floatForKey:@"strokeValue"];
                    //   NSLog(@"strokeT=%f",strokeT);
                    UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
                    [imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
                    //   NSLog(@"STROKE %f",stroke);
                    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), strokeT);
                    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
                    float redT=[[NSUserDefaults standardUserDefaults] floatForKey:@"redvalue"];
                    float greenT= [[NSUserDefaults standardUserDefaults] floatForKey:@"greenvalue"];
                    float blueT= [[NSUserDefaults standardUserDefaults] floatForKey:@"bluevalue"];

                    //   NSLog(@"red=%f",redT);
                    //   NSLog(@"green=%f",greenT);
                    //   NSLog(@"blue=%f",blueT);

                    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redT, greenT, blueT, 1.0);
                    CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), redT, greenT, blueT, 1.0);

                    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
                    CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
                    CGContextBeginPath(UIGraphicsGetCurrentContext());
                    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
                    CGContextStrokePath(UIGraphicsGetCurrentContext());
                    imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                }
                lastPoint = currentPoint;
            }
        }
    }

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


        //if (([touch view] != btnInkColor) || ([touch view] != btnPenSize) || ([touch view] != baseview)) {
        if ( [touch view] != baseview) {

            if (!isImageSelection) {

                if(!mouseSwiped) {
                    if (isEraser) {
                        UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
                        [imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
                        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
                        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), stroke);
                        // CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
                        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
                        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                        CGContextStrokePath(UIGraphicsGetCurrentContext());
                        CGContextFlush(UIGraphicsGetCurrentContext());
                        imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
                        UIGraphicsEndImageContext();
                    } else {
                        UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
                        [imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];

                        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), stroke);
                        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

                        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
                        CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);

                        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
                        CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
                        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                        CGContextStrokePath(UIGraphicsGetCurrentContext());
                        CGContextFlush(UIGraphicsGetCurrentContext());
                        imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
                        UIGraphicsEndImageContext();
                    }
                }
            }
        }
    }
}

我该怎么解决这个问题?


我不知道你是怎么想出这个想法的。但是绘图的代码应该在drawRect方法中而不是touch方法中。 - CRDave
通过使用CGMutablePathRef,我解决了上述问题。 - itechnician
你能否通过上面我放置的一些代码来展示一下?感谢您的回复。 - Manthan
3个回答

10

为了寻找即时解决方案,请替换此行。

 mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());
[mainImage performSelectorInBackground:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];
如果您需要一个精细而准确的解决方案,请尝试将MoveTo替换为CGMutablepath。 希望这可以帮助您。

3
我们使用在touchMoved方法中添加dispatch_async(dispatch_get_main_queue(), ^{)}代码块来解决这个问题,具体如下:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

        dispatch_async(dispatch_get_main_queue(), ^{
        mouseSwiped = YES;
        UITouch *touch = [touches anyObject];
        if(touch.view==self.vwSwipePage)
        {
            return;
        }
        if(flagIsEraser)
        {
            CGPoint currentPoint;
            if(flagActiveViewPage)
            {

                currentPoint = [touch locationInView:self.mainImage];
                UIGraphicsBeginImageContext(self.mainImage.frame.size);
                [self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
            }
            else
            {
                currentPoint = [touch locationInView:self.mainImage1];
                UIGraphicsBeginImageContext(self.mainImage1.frame.size);
                [self.mainImage1.image drawInRect:CGRectMake(0, 0, self.mainImage1.frame.size.width, self.mainImage1.frame.size.height)];
            }

            //clear using circle brush........
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineWidth(context,20);
            CGContextSetBlendMode(context, kCGBlendModeClear);
            CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
            CGContextBeginPath(context);
            CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
            CGContextStrokePath(context);
            CGContextFlush(context);
            if(flagActiveViewPage)
                self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
            else
                self.mainImage1.image = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();
            lastPoint = currentPoint;

        }
    });
    }

希望这可以帮助你解决问题。

谢谢。


好的,我会尝试并告诉您。谢谢您的回复。 - Manthan
非常感谢,它有点帮助,但如果您替换那行代码,技术人员提到的那个是完美的。+1 感谢您的帮助。 - Manthan
我也用了itechnician提到的这行代码进行了检查,真的对我也很有效...谢谢itechnician - puja
之前我提到的方法可以正常工作,但是在使用AddQuadCurveToPoint时,它只显示直线而不是平滑曲线。所以这很完美。再次感谢itechnician... - puja

0

但您能否请提供一下如何将这段代码放入“绘制矩形”方法中的建议? - Manthan
我现在正在寻找答案 :) 一旦我明白了,就会尽快发布它。 - Kostiantyn Sokolinskyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接