在绘制ios7时滞后 [英] Lag while drawing in ios7

查看:93
本文介绍了在绘制ios7时滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序,我正在对视图进行一些草图绘制。

I am having an app in which I am doing some sketching on a view.

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

So far, it was working fine until I installed ios7.

我的应用程序使用触摸移动方法识别运动的变化。但是当我绘制一条线时,触摸方法会被调用但是直到我触摸 ios7 结束时才会更新。

My app uses touches moved method for recognizing the change of a movement. But when I draw a line, touches method gets called but line doesn't get updated until I touch ends in ios7.

所以绘画有一点滞后。

So there is a slight lag in drawing.

它适用于 ios6 ios7 模拟器但是当我在真正的 ios7 设备上进行测试时,绘图算法存在滞后现象。

It works fine on ios6 and on ios7 simulator but when i test it on a real ios7 device, there is a lag in drawing algorithm.

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

我该如何解决这个问题?

How can i solve this?

推荐答案

要找到即时解决方案,请替换此行

To find a instant solution, Replace this line

 mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());

with

[mainImage performSelectorInBackground:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];

如果您需要精心准确的解决方案,请尝试使用CGMutablepath替换MoveTo。
希望这有帮助。

If you need an elaborate and accurate solution try to replace the MoveTo with CGMutablepath . Hope this helps.

这篇关于在绘制ios7时滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆