2013-10-16 71 views
1

我開發了一款iPad繪圖工具。在iOS7中使用CGContext繪製問題

在iOS6上使用該工具時,工作正常,但在iOS7上使用該工具時,畫線不會在觸摸結束後出現。

我使用CGContextAddLineToPointUIGraphicsGetCurrentContext進行繪製。

感謝

這是代碼

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    pointerUndone++; 
// NSLog(@"undone store %f",pointerUndone); 

    UITouch *touch = [[event allTouches] anyObject]; 

    if (DEBUG_MESSAGES == YES) NSLog(@"Draw"); 

    if([touch tapCount] == 2){ 
     //drawImage.image = nil;//double touch for clear all current stroke 
    }else if([touch tapCount] == 3){ 
     [self finishParentView]; 
    } 

    self.currentStroke = [[NSMutableArray alloc] init]; 

    location = [touch locationInView:touch.view]; 
    lastClick = [NSDate date]; 

    lastPoint = [touch locationInView:self.view]; 
    lastPoint.y -= 0; 

    [self.currentStroke addObject:[NSValue valueWithCGPoint:lastPoint]]; 

    [super touchesBegan:touches withEvent:event]; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self.view]; 



    UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)); 
    [drawImage.image drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt); 

    if(self.draftState == NO){ 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), self.sizeStroke); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), R/255, G/255, B/255, alpha); //draw 
    }else{ 

     if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
     else 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25); 
     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); //delete 
    } 

    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    [drawImage setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)]; 

    //drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

    drawImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext()); 

    UIGraphicsEndImageContext(); 
    lastPoint = currentPoint; 

    [self.view addSubview:drawImage]; 

    [self.currentStroke addObject:[NSValue valueWithCGPoint:currentPoint]]; 

} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    if([self.currentStroke count]>1){ 
     DatabaseManager *db = [[DatabaseManager alloc] init]; 
     [db deleteRedone]; 
     NSMutableDictionary *strokeWithColor = [[NSMutableDictionary alloc] init]; 
     NSDictionary *colorDictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithDouble:R],[NSNumber numberWithDouble:G],[NSNumber numberWithDouble:B], nil] forKeys:[NSArray arrayWithObjects:@"R",@"G",@"B", nil]]; 
     [strokeWithColor setObject:[NSString stringWithFormat:@"%.2f",self.sizeStroke] forKey:@"size"]; 
     if(self.draftState == NO){ 
      [strokeWithColor setObject:@"pencil" forKey:@"type"]; 
      [strokeWithColor setObject:[self getSelectedColor] forKey:@"colorString"]; 
     }else{ 
      [strokeWithColor setObject:@"draft" forKey:@"type"]; 
      [strokeWithColor setObject:@"TRANSPARENT" forKey:@"colorString"]; 
     } 

     [strokeWithColor setObject:colorDictionary forKey:@"color"]; 
     //[strokeWithColor setObject:[self getSelectedColor] forKey:@"colorString"]; 
     [strokeWithColor setObject:self.currentStroke forKey:@"stroke"]; 
     [strokeWithColor setObject:@"YES" forKey:@"visible"]; 

     [self.strokes addObject:strokeWithColor]; 
     [self registerStrokeInDataBase:self.currentStroke]; 
    } 
} 
+1

你能展示一些代碼嗎?你在哪裏調用'CGContextAddLineToPoint'和'UIGraphicsGetCurrentContext'? –

+0

您還應該顯示觸摸結束代碼。 – Rob

+1

不要用這個代碼:'[self.view addSubview:drawImage];'在touchMoved中。您將每分鐘刪除並重新添加單個視圖到您的視圖數百次。 – Putz1103

回答

3

爲了找到一個即時的解決方案,替換該行

drawImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext()); 

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

如果你需要一個複雜的和準確的解決方案嘗試用CGMutablepath替換MoveTo。 希望這有助於。

+0

謝謝!!這個解決方案非常完美 –

+0

@JuanVidal如果有幫助,請接受答案 – itechnician