Iphone 在ios7中绘制时滞后

Iphone 在ios7中绘制时滞后,iphone,ios,objective-c,drawing,ios7,Iphone,Ios,Objective C,Drawing,Ios7,我有一个应用程序,在其中我在视图上做一些草图 到目前为止,它工作正常,直到我安装了ios7 我的应用程序使用触摸移动方法来识别移动的变化。但当我画一条线时,会调用touch方法,但直到touch在ios7中结束时,该线才得到更新 所以在绘图上有点滞后 它在ios6和ios7模拟器上运行良好,但当我在真正的ios7设备上测试它时,绘图算法存在滞后 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (!isIm

我有一个应用程序,在其中我在视图上做一些草图

到目前为止,它工作正常,直到我安装了
ios7

我的应用程序使用触摸移动方法来识别移动的变化。但当我画一条线时,会调用touch方法,但直到touch在
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();
                    }
                }
            }
        }
    }
}

如何解决这个问题?

这里列出了ios7上出现相同问题的相同代码。人们建议将绘图逻辑移到drawRect中:

我们使用
dispatch\u async(dispatch\u get\u main\u queue(),^{)解决了这个问题touchMoved
方法中的code>block如下:

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

        }
    });
    }
愿这能帮助你解决你的问题


谢谢

要找到即时解决方案,请更换此线路

 mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());

如果您需要详细而准确的解决方案,请尝试用CGMutablepath替换MoveTo。
希望这能有所帮助。

我不知道你是怎么想到这个主意的。但是绘图的代码应该是drawRect方法而不是touch方法。通过使用CGMutablePathRef,我解决了上面的问题。你能通过我上面放的一些代码来显示这一点吗?谢谢你的回复。但是你能建议如何把这段代码放在draw-rect方法中吗?我现在自己在寻找答案:)我会在意识到如何时尽快发布,我会尝试让你知道。谢谢你的回复。非常感谢,这有点帮助,但如果你更换了it技术人员提到的那条线路,那就太完美了+1谢谢你的帮助。我也检查了使用itechnician提到的这条线,真的对我也很好…感谢itechnician之前的方法,我提到过它很好,但使用AddQuadCurveToPoint时,它只显示线而不是平滑线…所以这很完美…再次感谢itechnician。。。
[mainImage performSelectorInBackground:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];