- (UIImage *) buttonImageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
本文介绍了一个使用SwiftUI实现的函数,该函数能够根据提供的UIColor生成指定大小的按钮图片。通过UIGraphics和CoreGraphics API,实现从颜色到图像的转换,为开发者提供了一种简便的方法来创建具有特定颜色的按钮图像资源。

1万+

被折叠的 条评论
为什么被折叠?



