Question

Is there a way how can I cut a hole in my Image. What I want is to cut the eyes and mouth. I want it to become transparent.

Was it helpful?

Solution

Make a UIBezierPath will all the vertices of the closed path that you want to make transparent. The bezier path is made like this:

UIBezierPath *currentPath = [UIBezierPath bezierPath];
CGPoint tempPoint = CGPointMake(0,0);
[currentPath moveToPoint:CGPointMake(tempPoint.x, tempPoint.y)];

tempPoint = CGPointMake(0,20);
[currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)];
tempPoint = CGPointMake(20,20);
[currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)];   
tempPoint = CGPointMake(20,0);
[currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)];
[currentPath closePath];

and then use the following code to make that area transparent and save in a new image

// Create an image context containing the original UIImage.
UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawAtPoint:CGPointZero];

// Clip to the bezier path and clear that portion of the image.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context,currentPath.CGPath)
CGContextClip(context);
CGContextClearRect(context,CGRectMake(0,0,originalImage.size.width,originalImage.size.height);

// Build a new UIImage from the image context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

OTHER TIPS

try this one

firstly add the quartzcore

 #import <QuartzCore/QuartzCore.h>

Create the path you want to clip

 UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:yourView.bounds];
    [maskPath appendPath:[UIBezierPath bezierPathWithRect:rectToPunchThroughTheView]];

Then create a mask layer and add it to your view - CAShapeLayer will allow you to define any path

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillRule  = kCAFillRuleEvenOdd;
maskLayer.fillColor = [UIColor blackColor].CGColor;
maskLayer.path      = maskPath.CGPath;

yourView.layer.mask = maskLayer;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top