我正在尝试从我拥有的这个 UIColor 值创建一个 UIImage 。UIImage 正在创建,但 alpha 值丢失。图像保持不透明。我正在使用以下代码:
@implementation GetImage
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
UIImage *img = nil;
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
color.CGColor);
CGContextFillRect(context, rect);
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
我这样称呼:
-(void) setButtonColours{
UIImage *img1, *img2;
UIColor *red = [UIColor colorWithRed:205.0/255.0 green:68.0/255.0 blue:51.0/255.0 alpha:1.0],
*blue = [UIColor colorWithRed:71.0/255.0 green:97.0/255.0 blue:151.0/255.0 alpha:1.0];
img1 = [GetImage imageWithColor: blue andSize: self.dummyButton.frame.size];
img2 = [GetImage imageWithColor: red andSize: self.dummyButton.frame.size];
[self.dummyButton setBackgroundImage: img1 forState:UIControlStateNormal];
[self.dummyButton setBackgroundImage: img2 forState:UIControlStateSelected];
}
知道我错过了什么吗?