Ios 在更改图像颜色的同时更改CGContext的线宽?
我正在尝试更改使用CG绘制的矩形的宽度和颜色。在下面的函数中,我使用不同的颜色遮罩图像,但如何更改宽度Ios 在更改图像颜色的同时更改CGContext的线宽?,ios,core-graphics,Ios,Core Graphics,我正在尝试更改使用CG绘制的矩形的宽度和颜色。在下面的函数中,我使用不同的颜色遮罩图像,但如何更改宽度 - (void)colorImage:(UIImage *)origImage withColor:(UIColor *)color withWidth:(float) width { UIImage *image = origImage; NSLog(@"%f", width); CGRect rect = CGRectMake(0, 0, image.size.width, image.s
- (void)colorImage:(UIImage *)origImage withColor:(UIColor *)color withWidth:(float) width
{
UIImage *image = origImage;
NSLog(@"%f", width);
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, width);
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *flippedImage = [UIImage imageWithCGImage:img.CGImage
scale:1.0 orientation: UIImageOrientationDownMirrored];
self.image = flippedImage;
}
您可以使用
CGContextSetLineWidth(上下文,宽度)
设置线宽
你之所以看不到任何效果,是因为你没有抚摸任何东西。线条宽度适用于通过笔划绘制的线条。你在填充,而不是抚摸,填充没有线条宽度
如果要在矩形周围放置边框,则需要对其进行笔划。这就是围绕某个形状的周长画一条线的原因
您有三种选择:
- 调用
,然后调用CGContextSetLineWidth
CGContextStrokeRect
- 调用
CGContextStrokeRectWithWidth
- 调用
,然后调用CGContextSetLineWidth
(将矩形添加到当前路径),然后使用CGContextAddRect
调用kCGPathFillStroke
。(或者在CGContextDrawPath
之前调用SetLineWidth
,如果您愿意,它们只需要在AddRect
之前发生)DrawPath
您还应该决定是否真的要填充,或者是否只需要笔划即可。您可以使用
CGContextSetLineWidth(context,width)
设置线宽
你之所以看不到任何效果,是因为你没有抚摸任何东西。线条宽度适用于通过笔划绘制的线条。你在填充,而不是抚摸,填充没有线条宽度
如果要在矩形周围放置边框,则需要对其进行笔划。这就是围绕某个形状的周长画一条线的原因
您有三种选择:
- 调用
,然后调用CGContextSetLineWidth
CGContextStrokeRect
- 调用
CGContextStrokeRectWithWidth
- 调用
,然后调用CGContextSetLineWidth
(将矩形添加到当前路径),然后使用CGContextAddRect
调用kCGPathFillStroke
。(或者在CGContextDrawPath
之前调用SetLineWidth
,如果您愿意,它们只需要在AddRect
之前发生)DrawPath