IOS:带有图案图像的颜色

IOS:带有图案图像的颜色,ios,xcode,touch,design-patterns,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.siz

我用一个简单的行给这个代码上色:

- (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;你能帮我吗?

如果你想画图案(平铺图像),请查看

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

如果要绘制图案(平铺图像),请签出


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

请尝试
colorWithPatternImage

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

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

试试
colorWithPatternImage

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

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

我不想画瓷砖图像,但我想用手指画一个图像;我的代码画了一条线;相反,我想画一个图像(例如png),对不起,我误解了。您可以使用
CGContextDrawTiledImage
UIImage:drawInRect
在特定位置绘制图像。但我先前的建议仍然有效。您最好在UI事件期间记录要绘制的点(使用NSArray存储点),然后在点上循环并在
drawRect
中绘制图像我不想绘制平铺图像,但我想用手指绘制图像;我的代码画了一条线;相反,我想画一个图像(例如png),对不起,我误解了。您可以使用
CGContextDrawTiledImage
UIImage:drawInRect
在特定位置绘制图像。但我先前的建议仍然有效。您最好在UI事件期间记录要绘制的点(使用NSArray存储点),然后在点上循环并在
drawRect
中绘制图像您是否尝试过使用
-(UIColor*)initWithPatternImage:(UIImage*)image
?您是否尝试过使用
-(UIColor*)initWithPatternImage:(UIImage*)image