xcode IOS:带有图案图像的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9790078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: color with a pattern image
提问by cyclingIsBetter
I have this code to colour with a simply line:
我有这个代码用一条简单的线来着色:
- (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;
}
with this code I'm able to color in a view or imageview; I can choose color and size; but now I want to use a specific pattern to color this view; a specific png to use when touchmoved is called; can you help me?
使用此代码,我可以在视图或图像视图中着色;我可以选择颜色和尺寸;但现在我想使用特定的图案来为这个视图着色;调用 touchmoved 时要使用的特定 png;你能帮助我吗?
回答by He Shiming
Check out https://stackoverflow.com/a/707329/108574if you want to draw pattern (tiled images).
如果您想绘制图案(平铺图像),请查看https://stackoverflow.com/a/707329/108574。
However, you are doing it the wrong way in your code. You aren't supposed to draw to the graphics context during UI events. Drawing routines should be inside - (void)drawRect:(CGRect)rect
of a UIView instance. When a UI event refreshes graphics, you record the new state somewhere, and then issue - (void)setNeedsDisplay
message to the UIView instance to redraw.
但是,您在代码中以错误的方式执行此操作。您不应该在 UI 事件期间绘制到图形上下文。绘图例程应该在- (void)drawRect:(CGRect)rect
UIView 实例内。当 UI 事件刷新图形时,您将新状态记录在某处,然后- (void)setNeedsDisplay
向 UIView 实例发出消息以进行重绘。
回答by Frade
Try colorWithPatternImage
:
尝试colorWithPatternImage
:
Creates and returns a color object using the specified image.
使用指定的图像创建并返回一个颜色对象。
+ (UIColor *)colorWithPatternImage:(UIImage *)image