gpt4 book ai didi

ios - 将 UIImage 创建为带颜色的边框

转载 作者:行者123 更新时间:2023-11-28 10:22:23 25 4
gpt4 key购买 nike

我正在使用 FSCalendar我想做的是改变显示事件的方式。我想在带有事件的单元格周围放置一个彩色矩形边框。我通过编辑单元格背景层来做到这一点并且工作正常,但现在我意识到将我的代码更新到最新版本的 FSCalendar 是错误的地方,这将覆盖我的更改。日历代表可以访问的内容之一是将图像设置为单元格,因此我想将图像创建为带有事件颜色的矩形边框。这是我想要的图像: Calendar With events as rectangle border

任何建议都会被采纳。提前致谢。

最佳答案

此方法将绘制一个带边框的矩形。我会把它作为类方法并放入 UIImage 类别中以方便使用。

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)imageSize andBorderWidth:(CGFloat)borderWidth fillWithColor:(BOOL)fillWithColor{

UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();

CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);

if(fillWithColor) {
[color setFill];
CGContextFillRect(context, rect);
} else {
[color setStroke];
CGContextStrokeRectWithWidth(context, rect, borderWidth);
}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

编辑:添加了 fillWithColor 参数

关于ios - 将 UIImage 创建为带颜色的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com