0

I found this code here, but am unsure how to use it.

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

Am I subclassing a UIImage? Where does drawRect need to be called? Thanks

Community
  • 1
  • 1
davis
  • 1,911
  • 6
  • 26
  • 50
  • Subclass `UIView` to get `drawRect:` working. The system will call `drawRect:` when it needs to. No need to manually invoke it. – Till Jun 23 '13 at 23:48
  • So, when I am initializing my UIImage, how do I go about using my UIView subclass? – davis Jun 23 '13 at 23:49
  • Not sure if I am getting your question but you would commonly initialize the image (`myImage`) within your view's initializer (`initWithFrame` and/or `initWithCoder`). – Till Jun 23 '13 at 23:52
  • I would agree, I ended up using the solution linked by @Till – davis Jun 25 '13 at 03:54

1 Answers1

0

Hope will help you.

#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;
}
bachle26
  • 89
  • 1
  • 10
  • I found this [answer](http://stackoverflow.com/a/4630136/873546) to be far more effective because it creates a UIImage category and allows additional UIImage methods to be accessed. – davis Jun 24 '13 at 05:00