首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios7绘图时的滞后现象

ios7绘图时的滞后现象
EN

Stack Overflow用户
提问于 2013-10-01 04:46:06
回答 3查看 1.6K关注 0票数 3

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

到目前为止,在我安装ios7之前,它还可以正常工作。

我的应用程序使用触摸移动方法来识别运动的变化。但是,当我画一条线时,会调用touch方法,但只有在我接触到ios7结束时,才会更新行。

因此,在绘画上有一个轻微的滞后。

它在ios6ios7模拟器上工作得很好,但是当我在一个真正的ios7设备上测试它时,绘图算法还存在一定的滞后性。

代码语言:javascript
复制
- (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();
                    }
                }
            }
        }
    }
}

我怎么才能解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-21 05:10:22

若要找到即时解决方案,请替换以下行

代码语言:javascript
复制
 mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());

使用

代码语言:javascript
复制
[mainImage performSelectorInBackground:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];

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

票数 9
EN

Stack Overflow用户

发布于 2013-10-16 08:48:30

我们在dispatch_async(dispatch_get_main_queue(), ^{)};方法中使用touchMoved块解决了这个问题,如下所示:

代码语言:javascript
复制
- (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;

        }
    });
    }

希望这能帮助你解决你的问题。

谢谢

票数 3
EN

Stack Overflow用户

发布于 2013-10-01 12:56:52

下面列出了在ios7 https://stackoverflow.com/questions/18198129/ios-7-making-my-apps-drawing-algorithm-lag上具有相同问题的相同代码,人们建议将绘制逻辑移到drawRect中:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19108185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档