CGSize imageSize =CGSizeMake(50,50);
UIGraphicsBeginImageContextWithOptions(imageSize,0, [UIScreenmainScreen].scale);
[RGBA(206,58, 216, 1) set];
UIRectFill(CGRectMake(0,0, imageSize.width, imageSize.height));
UIImage *pressedColorImg =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
或者:
You will want to clip the context to an image mask and then fill with a solid color:
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
[[UIColor blackColor] set];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, bounds, [myImage CGImage]);
CGContextFillRect(context, bounds);
}
Note: myImage
should be an instance variable that contains an UIImage
. I'm not sure whether it takes the mask from the alpha channel or the intensity so try both.