1
其实我们在涂料中的应用工作,如何使用图像绘制UIBezierPath,删除图像
我们做笔触使用贝塞尔路径,路径数组..我们几乎完成申请,
但有是一些性能问题drayong画笔画从PathsArray有alredy绘制Bezierpaths,就像绘制笔画变得缓慢后,绘制一些笔画,
因此,为避免这个性能问题,我们在sketchImage视图背后使用了temperary iamge视图,
我们最近绘制的素描顶视图图像一举并更新与PathsArray底部Temperary iamge观点,
它工作正常,性能imrove,但在橡皮擦的问题..
虽然这样做橡皮擦在背景和temperary iamge视图更新只在行程结束时,
所以我们计划消除图像时橡皮擦选择,如何做到这一点..任何其他方式
-(void)updateTempararyBottomLayerImageView
{
TempararyBottomLayerImageView.frame = CGRectMake(TopLayerImageView.frame.origin.x, TopLayerImageView.frame.origin.y, TopLayerImageView.frame.size.width, TopLayerImageView.frame.size.height);
UIGraphicsBeginImageContext(TempararyBottomLayerImageView.bounds.size);
for (NSDictionary *dict in pathsArray)
{
UIBezierPath *p = (UIBezierPath*)[dict objectForKey:@"path"];
p.lineWidth = [[dict objectForKey:@"size"]floatValue];
if ([[dict objectForKey:@"red"] floatValue] == 0) //When eraser selected
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
[[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue]
green:[[dict objectForKey:@"green"]floatValue]
blue:[[dict objectForKey:@"blue"]floatValue]
alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke];
}
[p stroke];
}
[[UIColor colorWithRed:red green:green blue:blue alpha:opacity] setStroke];
if(isEraser) //When eraser selected, change color as clear
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
}
if (!isTouchEnd)
[bzierPath stroke];
TempararyBottomLayerImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (void) updateDrawingBoard
{
UIGraphicsBeginImageContext(TopLayerImageView.bounds.size);
if (pathsArray.count > 0)
{
NSDictionary * dict = [pathsArray lastObject];
UIBezierPath *p ;
@try {
p = (UIBezierPath*)[dict objectForKey:@"path"];
}
@catch (NSException *exception)
{
NSLog(@"xxxxxxxxxxxx");
return;
}
p.lineWidth = [[dict objectForKey:@"size"]floatValue];
if ([[dict objectForKey:@"red"] floatValue] == 0) //When eraser selected
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
[[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue]
green:[[dict objectForKey:@"green"]floatValue]
blue:[[dict objectForKey:@"blue"]floatValue]
alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke];
}
[p stroke];
[[UIColor colorWithRed:red green:green blue:blue alpha:opacity] setStroke];
if(isEraser) //When eraser selected, change color as clear
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
}
if (!isTouchEnd)
[bzierPath stroke];
self.TopLayerImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
# pragma =====UITouch delegates=====
//==============================================
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([[event touchesForView:self.TopLayerImageView] count] == 1)
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.TopLayerImageView];
bzierPath = [UIBezierPath bezierPath];
bzierPath.lineCapStyle = kCGLineCapRound;
bzierPath.lineJoinStyle = kCGLineJoinRound;
bzierPath.lineWidth = brushSize;
[bzierPath moveToPoint:touchPoint];
isTouchEnd = NO;
UIBezierPath *tempPath = bzierPath;
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
tempPath,@"path",
[NSNumber numberWithFloat:red], @"red",
[NSNumber numberWithFloat:green], @"green",
[NSNumber numberWithFloat:blue], @"blue",
[NSNumber numberWithFloat:opacity], @"alpha",
[NSNumber numberWithFloat:brushSize], @"size", nil];
if (isEraser)
{
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
tempPath,@"path",
[NSNumber numberWithFloat:0], @"red",
[NSNumber numberWithFloat:0], @"green",
[NSNumber numberWithFloat:0], @"blue",
[NSNumber numberWithFloat:0], @"alpha",
[NSNumber numberWithFloat:brushSize], @"size", nil];
}
[pathsArray addObject:dict];
[self updateDrawingBoard];
}
//To hide bottom scroll wheel where ever tap in view
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] != bottomScrollWheel)
{
[self minimizeScrollWheel];
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if([[event touchesForView:self.TopLayerImageView] count] == 1)
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.TopLayerImageView];
[bzierPath addLineToPoint:touchPoint];
[bzierPath moveToPoint:touchPoint];
isTouchEnd = NO;
[self updateDrawingBoard];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if([[event touchesForView:self.TopLayerImageView] count] == 1)
{
// NSLog(@"**********************bzierPath == \r\n %@",bzierPath);
CGPoint touchPoint = [[touches anyObject] locationInView:self.TopLayerImageView];
[bzierPath moveToPoint:touchPoint];
[bzierPath addLineToPoint:touchPoint];
//bzierPath = nil;
isTouchEnd = YES;
[self updateDrawingBoard];
[self updateTempararyBottomLayerImageView];
if (TopLayerImageView.image != nil)
{
[undoArray addObject:TempararyBottomLayerImageView.image];
[redoArray removeAllObjects];
[redoPathsArray removeAllObjects];
}
self.TopLayerImageView.image = nil;
[self EnableDisableUndoRedo];
}
}
# pragma mark =====Settings =====
//==============================
// Slider Position can be changed from left to right or right to left
-(void)sliderPositionChange
{
if (settingsPopoverVC.index == 0)
{
if (iOS7)
[sliderView setFrame:CGRectMake(19, 128, 100, 357)];
else
[sliderView setFrame:CGRectMake(19, 108, 100, 357)];
}
else if(settingsPopoverVC.index == 1)
{
if (iOS7)
[sliderView setFrame:CGRectMake(900, 128, 100, 357)];
else
[sliderView setFrame:CGRectMake(900, 108, 100, 357)];
}
}
你尝试用[自setNeedDisplay] – codercat
我硝基甲苯知道究竟相关[自setNeedDisplay]你能否详细解释一下 。 – user2732294