IOS: drawing only parts of UImage

I am trying to draw only the user part UIImage(i.e.: I would like to show the user touch part UIImage), and I have reasonable results using the property maskfor layer.

Something like this on my UIView:

UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath setLineWidth:10.0];
[maskPath moveToPoint:CGPointMake(10.0, 10.0)];
[maskPath addLineToPoint:CGPointMake(100.0, 100.0)];
CAShapeLayer *shapeMaskLayer = [CAShapeLayer layer];
shapeMaskLayer.path = maskPath.CGPath;
[self.layer setMask:shapeMaskLayer];

Then to drawRect:

- (void)drawRect:(CGRect)rect
{
    [img drawInRect:rect];
}

It works. I just see the part of the image being detected maskPath.

However, this does not seem to be the best way to approach this issue. So my question is: What would be the best way to draw only the user part (it can be any form) of the image in the iOS SDK? .

+3
source share
1 answer

, , - . .

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    UIBezierPath *maskPath = [UIBezierPath bezierPath];
    [maskPath setLineWidth:10.0];
    [maskPath moveToPoint:CGPointMake(10.0, 10.0)];
    [maskPath addLineToPoint:CGPointMake(100.0, 100.0)];

    CGContextAddPath(ctx, maskPath.CGPath);
    CGContextClip(ctx)
    [img drawInRect:rect];
}
+4

All Articles