Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS make part of an UIImage transparent

Tags:

ios

uiimage

I have an UIImage where part of it has been selected by the user to clear out (make transparent). To make the selection I used NSBezierPath.

How can I clear/make transparent part of an UIImage in iOS?

like image 559
MB. Avatar asked Nov 29 '11 03:11

MB.


1 Answers

First, I assume you have a UIBezierPath (iOS), not an NSBezierPath (Mac OS X).

To do this, you will need to use core graphics, creating an image context, drawing the UIImage into that context, and then clearing the region specified by the NSBezierPath.

// 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,bezierPath.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();
like image 57
landweber Avatar answered Oct 09 '22 19:10

landweber