在 iOS7 中使用 CGContext 绘制问题
Posted
技术标签:
【中文标题】在 iOS7 中使用 CGContext 绘制问题【英文标题】:Problems drawing with CGContext in iOS7 【发布时间】:2013-10-16 13:20:10 【问题描述】:我为 iPad 开发了一个绘图工具。
在 ios6 上使用该工具时,一切正常,但在 iOS7 上使用该工具时,触摸结束后不会出现画线。
我正在使用CGContextAddLineToPoint
和UIGraphicsGetCurrentContext
进行绘图。
谢谢
这是代码
-(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];
【问题讨论】:
你能显示一些代码吗?你在哪里打电话给CGContextAddLineToPoint
和UIGraphicsGetCurrentContext
?
您还应该显示触摸结束代码。
废话,不要在 touchMoved 中使用此代码:[self.view addSubview:drawImage];
。您将每分钟数百次将该单个视图删除并重新添加到您的视图中。
正确的方法是什么?
【参考方案1】:
要找到即时解决方案,请替换此行
drawImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());
与
[drawImage performSelectorInBackground:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];
如果您需要详细而准确的解决方案,请尝试将 MoveTo 替换为 CGMutablepath 。 希望这会有所帮助。
【讨论】:
以上是关于在 iOS7 中使用 CGContext 绘制问题的主要内容,如果未能解决你的问题,请参考以下文章