希望能帮到你。
#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;
}