3

I have a UIImageView on UIView. On that I have drawn a shape using UIBezierPath. Now I want other part to be transparent. My code is,

- (void)drawRect:(CGRect)rect
{
    self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    aPath = [UIBezierPath bezierPath];
    CGContextRef aRef ;
    aRef = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] setStroke];
    [[UIColor clearColor] setFill];
    aPath.lineWidth = 2;
    aPath = [UIBezierPath new];
                //path is calculated here using addLineToPoint addArcWithCentre methods
    [aPath fill];
    [aPath stroke];
    [self setClippingPath:aPath :imgV];
    [[[UIColor clearColor] colorWithAlphaComponent:0.0] setFill];
    CGContextFillRect(aRef, rect);
    CGContextFillPath(aRef);
    self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
}
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{   
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imgView.frame;
    maskLayer.path = [clippingPath CGPath];
    maskLayer.backgroundColor = [[[UIColor clearColor] colorWithAlphaComponent:0.0] CGColor];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];

    [imgView removeFromSuperview];
    imgView.layer.mask = maskLayer;

    [self addSubview:imgView];

}   

Output can be seen in following image. Black part around that image cur should be transparent enter image description here Please help me.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Durgaprasad
  • 1,910
  • 2
  • 25
  • 44

1 Answers1

0

I think you might be missing one last call, probably right before addSubview:

imgView.clipsToBounds = YES;
Noah Dyer
  • 407
  • 5
  • 10