IOS:带图案图像的颜色

时间:2012-03-20 15:44:56

标签: ios xcode touch design-patterns

我有这个代码用简单的颜色着色:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:drawImage];

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);


//eraser
/*
if (eraser){
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
}*/

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0); //black

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

使用此代码我可以在视图或imageview中着色;我可以选择颜色和大小;但现在我想使用特定的图案为这个视图着色;调用touchmoved时要使用的特定png;你能救我吗?

2 个答案:

答案 0 :(得分:2)

如果您想绘制图案(平铺图像),请查看https://stackoverflow.com/a/707329/108574

但是,您在代码中的方式是错误的。在UI事件期间,您不应该绘制到图形上下文。绘图例程应该在UIView实例的- (void)drawRect:(CGRect)rect内。当UI事件刷新图形时,您在某处记录新状态,然后向UIView实例发出- (void)setNeedsDisplay消息以重绘。

答案 1 :(得分:1)

尝试colorWithPatternImage

使用指定的图像创建并返回颜色对象。

+ (UIColor *)colorWithPatternImage:(UIImage *)image

(reference)