3

我有一个UIImageViewUIView在那我已经使用UIBezierPath. 现在我希望其他部分是透明的。我的代码是,

- (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];

}   

输出如下图所示。该图像周围的黑色部分应该是透明的在此处输入图像描述 请帮助我。

4

1 回答 1

0

我认为您可能错过了最后一个电话,可能就在 addSubview 之前:

imgView.clipsToBounds = YES;
于 2013-05-10T02:37:35.933 回答