2013-10-01 142 views
3

我有一个应用程序,在该应用程序中对视图进行一些素描。在绘制ios7时滞后

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

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

所以绘图有一点滞后。

它在ios6ios7模拟器上正常工作,但是当我在真实的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(); 
        } 
       } 
      } 
     } 
    } 
} 

我该如何解决这个问题?

+0

我不知道你是如何得到这个想法的。但是绘图代码应该在drawRect方法中不是触摸方法。 – CRDave

+0

通过使用CGMutablePathRef,我解决了上述问题。 – itechnician

+0

你能通过我上面提到的一些代码来展示这个吗?感谢您的回复。 – Manthan

回答

9

为了找到一个即时解决,如果你需要一个复杂而精确的解决方案试图取代与CGMutablepath对moveTo与

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

替换该行

mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext()); 

。 希望这有助于。

+1

非常感谢你itechnician ...你解决了我的问题。 – Manthan

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; 

     } 
    }); 
    } 

愿这意志帮助你解决你的问题。

感谢

+0

好吧,我会尽力让你知道。感谢您的回复。 – Manthan

+0

非常感谢你的帮助,但是如果你更换了那个技术人员提到的那条线是完美的。 +1为您提供帮助。 – Manthan

+0

我也检查过使用这个线哪个itechnician提到,真的它适用于我也罚款...谢谢itechnician – puja