我找到了这段代码here,但我不确定如何使用它。
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
[[UIColor blackColor] set];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, bounds, [myImage CGImage]);
CGContextFillRect(context, bounds);
}
我是否继承了UIImage? drawRect需要在哪里调用?感谢
答案 0 :(得分:0)
#pragma mark - Fill color To Image
-(void)fillColorToImage:(SelectedObject *)obj Color:(UIColor *)tintColor{
UIGraphicsBeginImageContextWithOptions(obj.image.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, obj.image.size.width, obj.image.size.height);
UIRectFill(bounds);
[obj.image drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0];
imgFillColor = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, obj.frame.size.width, obj.frame.size.height)];
imgFillColor.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view addSubview:imgFillColor];
// [self.view addSubview:imgVw];
imageview.image = [self maskImage:imageview.image withMask:imgFillColor.image];
[self.view addSubview:btnNav];
}
#pragma mark - mask
- (UIImage*) maskImage:(UIImage *) image withMask:(UIImage *) mask
{
CGImageRef imageReference = image.CGImage;
CGImageRef maskReference = mask.CGImage;
CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
CGImageGetHeight(maskReference),
CGImageGetBitsPerComponent(maskReference),
CGImageGetBitsPerPixel(maskReference),
CGImageGetBytesPerRow(maskReference),
CGImageGetDataProvider(maskReference),
NULL, // Decode is null
YES // Should interpolate
);
CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
CGImageRelease(imageMask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
CGImageRelease(maskedReference);
return maskedImage;
}