0

我在这里找到了这段代码,但不确定如何使用它。

- (void)drawRect:(CGRect)rect
{
    CGRect bounds = [self bounds];
    [[UIColor blackColor] set];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToMask(context, bounds, [myImage CGImage]);
    CGContextFillRect(context, bounds);
}

我是在继承 UIImage 吗?哪里需要调用drawRect?谢谢

4

1 回答 1

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;
}
于 2013-06-24T02:33:24.423 回答