iOS使UIImage的一部分透明化

MB.*_*MB. 11 uiimage ios

我有一个UIImage,其中部分内容被用户选中以清除(透明).为了进行选择,我使用了NSBezierPath.

如何在iOS中清除/制作UIImage的透明部分?

小智 14

首先,我假设您有一个UIBezierPath(iOS),而不是NSBezierPath(Mac OS X).

为此,您需要使用核心图形,创建图像上下文,将UIImage绘制到该上下文中,然后清除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();
Run Code Online (Sandbox Code Playgroud)