代码之家  ›  专栏  ›  技术社区  ›  Manthan

在ios7中绘制时滞后

  •  3
  • Manthan  · 技术社区  · 11 年前

    我有一个应用程序,我正在其中绘制视图。

    到目前为止,它运行良好,直到我安装 ios7 .

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

    我该如何解决这个问题?

    3 回复  |  直到 11 年前
        1
  •  10
  •   itechnician    11 年前

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

     mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());
    

    具有

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

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

        2
  •  3
  •   Hussain Shabbir    11 年前

    我们已经使用解决了这个问题 dispatch_async(dispatch_get_main_queue(), ^{)}; 闭塞 touchMoved 方法如下:

    - (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
  •  0
  •   Community Mr_and_Mrs_D    7 年前

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