【问题标题】:IOS: color with a pattern imageIOS:带有图案图像的颜色
【发布时间】:2012-03-20 15:44:55
【问题描述】:

我有这个代码可以用简单的线条着色:

- (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;
}

使用此代码,我可以在视图或图像视图中着色;我可以选择颜色和尺寸;但现在我想使用特定的模式来为这个视图着色;调用 touchmoved 时要使用的特定 png;你能帮帮我吗?

【问题讨论】:

  • 您是否尝试过使用- (UIColor *)initWithPatternImage:(UIImage *)image

标签: ios xcode touch design-patterns


【解决方案1】:

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

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

【讨论】:

  • 我不想画平铺图,但我想用手指画图;我的代码画了一条线;相反,我想绘制一个图像(例如 png)
  • 对不起,我误会了。您可以使用CGContextDrawTiledImageUIImage:drawInRect 在特定位置绘制图像。但我之前的建议仍然有效。你最好在 UI 事件期间记录你想要绘制的点(使用 NSArray 来存储点),然后循环这些点并在drawRect 中绘制图像
【解决方案2】:

试试colorWithPatternImage:

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

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

(reference)

【讨论】:

    猜你喜欢
    • 2013-11-17
    • 2022-06-22
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多