我试图改变使用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.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;
}
Run Code Online (Sandbox Code Playgroud)
用线设置线宽CGContextSetLineWidth(context, width).
你之所以没有看到效果,是因为你没有抚摸任何东西.线宽适用于通过描边绘制的线条.你正在填充,而不是抚摸,并且填充没有线来给出宽度.
如果要在矩形周围放置边框,则需要对其进行描边.这就是围绕某个形状的周边画一条线的原因.
你有三个选择:
CGContextSetLineWidth然后打电话CGContextStrokeRect.CGContextStrokeRectWithWidth.CGContextSetLineWidth,然后CGContextAddRect(以矩形添加到当前的路径),然后CGContextDrawPath用kCGPathFillStroke.(或者如果你愿意,可以AddRect先致电SetLineWidth- 他们只需要在两者之前发生DrawPath.)请注意,笔划以路径轮廓为中心,因此其中一半将位于路径/矩形内,一半位于路径外.如果你的线是1像素宽,这将显示为半透明的线(因为没有其他方式来表示"半像素").如果你的线是偶数个像素宽,并且你描边上下文(或视图)的整个边界,你只能看到里面的一半线.
你也应该决定你是否真的要填补,或者你是否只想要中风.
| 归档时间: |
|
| 查看次数: |
2905 次 |
| 最近记录: |