- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
            self.RealImage.frame = CGRectMake(12,137,740,850);
            
            self.imageView.frame = CGRectMake(12,137,740,850);
            self.tempImage.frame = CGRectMake(12,137,740,850);
      
    ctr = 0;
    UITouch *touch = [touches anyObject];
    pts[0] = [touch locationInView:self.tempImage];
    
    lastPoint = [touch locationInView:tempImage];
    
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    
   self.RealImage.frame = CGRectMake(12,137,740,850);
            
            self.imageView.frame = CGRectMake(12,137,740,850);
            self.tempImage.frame = CGRectMake(12,137,740,850);
    
    
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.tempImage];
    currentPoint = [touch locationInView:tempImage];
    ctr++;
    pts[ctr] = p;
   
    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); 
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];
        
        [self draw2];
        
        
        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
     NSLog(@"ctr:%d",ctr);
    lastPoint = currentPoint;
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    
    [path removeAllPoints];
    ctr = 0;
   
    
    
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    
  
    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImage.image = nil;
    
    
    
    UIGraphicsEndImageContext();
    
  
   
}
- (void)draw2
{
    
    
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
    
    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); 
    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];
    
    
   if ([pencilString isEqualToString:@"black"]) {
        [[UIColor blackColor] setStroke];
        [path setLineWidth:w];
    }
        
    else if ([pencilString isEqualToString:@"clear"]) {
        [[UIColor clearColor] setStroke];
        [path setLineWidth:w];
         CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
    }
   
    [path stroke];
    
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    
   
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:a];
   
    
    UIGraphicsEndImageContext();
    }
    
    
}