Ios 将UIImage着色为不同颜色,或从矢量生成UIImage
我有一个黑色轮廓的圆圈和一个白色填充,我需要通过UIColor编程将白色变成另一种颜色。我尝试过一些其他的stackoverflow解决方案,但没有一个能正确工作,要么只是填充外部,要么只是一个轮廓 我有两种方法可以做到这一点,但我不确定如何才能得到正确的结果: 将白色调成任何颜色 或者Ios 将UIImage着色为不同颜色,或从矢量生成UIImage,ios,uiimage,vector-graphics,Ios,Uiimage,Vector Graphics,我有一个黑色轮廓的圆圈和一个白色填充,我需要通过UIColor编程将白色变成另一种颜色。我尝试过一些其他的stackoverflow解决方案,但没有一个能正确工作,要么只是填充外部,要么只是一个轮廓 我有两种方法可以做到这一点,但我不确定如何才能得到正确的结果: 将白色调成任何颜色 或者 用两个圆制作一个UIImage,一个是填充的,另一个是用黑色重叠的。对于这样简单的形状,只需使用CoreGraphics绘制一个正方形和一个圆形,即可在实现中添加设置填充颜色的功能 如果它只是黑白的,那么当你知
用两个圆制作一个UIImage,一个是填充的,另一个是用黑色重叠的。对于这样简单的形状,只需使用CoreGraphics绘制一个正方形和一个圆形,即可在实现中添加设置填充颜色的功能 如果它只是黑白的,那么当你知道颜色表示时,把白色变成另一种颜色就不那么困难了。不幸的是,这是更复杂的编写和执行,所以…我的建议是直接去CoreGraphics的简单任务,你概述了糟糕的双关语,对不起 下面是一个快速演示:
static void InsetRect(CGRect* const pRect, const CGFloat pAmount) {
const CGFloat halfAmount = pAmount * 0.5f;
*pRect = CGRectMake(pRect->origin.x + halfAmount, pRect->origin.y + halfAmount, pRect->size.width - pAmount, pRect->size.height - pAmount);
}
static void DrawBorderedCircleWithWidthInContext(const CGRect pRect, const CGFloat pWidth, CGContextRef pContext) {
CGContextSetLineWidth(pContext, pWidth);
CGContextSetShouldAntialias(pContext, true);
CGRect r = pRect;
/* draw circle's border */
CGContextSetRGBStrokeColor(pContext, 0.8f, 0.7f, 0, 1);
InsetRect(&r, pWidth);
CGContextStrokeEllipseInRect(pContext, r);
/* draw circle's fill */
CGContextSetRGBFillColor(pContext, 0, 0, 0.3f, 1);
InsetRect(&r, pWidth);
CGContextFillEllipseInRect(pContext, r);
}
对于这样简单的形状,只需使用CoreGraphics绘制一个正方形和一个圆形,即可在实现中添加设置填充颜色的功能 如果它只是黑白的,那么当你知道颜色表示时,把白色变成另一种颜色就不那么困难了。不幸的是,这是更复杂的编写和执行,所以…我的建议是直接去CoreGraphics的简单任务,你概述了糟糕的双关语,对不起 下面是一个快速演示:
static void InsetRect(CGRect* const pRect, const CGFloat pAmount) {
const CGFloat halfAmount = pAmount * 0.5f;
*pRect = CGRectMake(pRect->origin.x + halfAmount, pRect->origin.y + halfAmount, pRect->size.width - pAmount, pRect->size.height - pAmount);
}
static void DrawBorderedCircleWithWidthInContext(const CGRect pRect, const CGFloat pWidth, CGContextRef pContext) {
CGContextSetLineWidth(pContext, pWidth);
CGContextSetShouldAntialias(pContext, true);
CGRect r = pRect;
/* draw circle's border */
CGContextSetRGBStrokeColor(pContext, 0.8f, 0.7f, 0, 1);
InsetRect(&r, pWidth);
CGContextStrokeEllipseInRect(pContext, r);
/* draw circle's fill */
CGContextSetRGBFillColor(pContext, 0, 0, 0.3f, 1);
InsetRect(&r, pWidth);
CGContextFillEllipseInRect(pContext, r);
}
扩展UIView并只实现drawRect方法。例如,这将绘制一个带有黑色轮廓的绿色圆圈
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef gc = UIGraphicsGetCurrentContext();
[[UIColor greenColor] setFill];
CGContextFillEllipseInRect(gc, CGRectMake(0,0,24,24));
[[UIColor blackColor] set];
CGContextStrokeEllipseInRect(gc, CGRectMake(0,0,24,24));
}
扩展UIView并只实现drawRect方法。例如,这将绘制一个带有黑色轮廓的绿色圆圈
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef gc = UIGraphicsGetCurrentContext();
[[UIColor greenColor] setFill];
CGContextFillEllipseInRect(gc, CGRectMake(0,0,24,24));
[[UIColor blackColor] set];
CGContextStrokeEllipseInRect(gc, CGRectMake(0,0,24,24));
}
如果您决定使用两个圆圈,一个白色,一个黑色,那么您可能会发现这很有用。此方法将为您的uiimage着色,但它解决了仅着色不透明部分的问题,这意味着,如果您在圆周围提供具有透明度的png,它将仅着色圆。因此,它不是填充整个24x24帧图像,而是只填充不透明部分。这不完全是你的问题,但如果你选择列出的第二个选项,你可能会遇到这个问题
-(UIImage*)colorAnImage:(UIColor*)color :(UIImage*)image{
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}如果您决定使用两个圆圈,一个白色,一个黑色,那么您可能会发现这很有帮助。此方法将为您的uiimage着色,但它解决了仅着色不透明部分的问题,这意味着,如果您在圆周围提供具有透明度的png,它将仅着色圆。因此,它不是填充整个24x24帧图像,而是只填充不透明部分。这不完全是你的问题,但如果你选择列出的第二个选项,你可能会遇到这个问题
-(UIImage*)colorAnImage:(UIColor*)color :(UIImage*)image{
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}你的意思是在drawRect中绘制圆还是使用某种图像?我需要制作一个24 x24的UIImage,带有UIColor’d圆和黑色边框,使用CoreGraphics动态绘制或在黑色版本上着色您的意思是在drawRect中绘制圆还是使用某种图像?我需要制作一个24 x24的UIImage,带有UIColor的圆和黑色边框,使用CoreGraphics动态绘制或在黑色版本上着色白色谢谢!我一直在寻找这样的解决方案,哈哈,谢谢!我一直在寻找这样的解决方案,哈哈。