问题 iOS使UIImage的一部分透明化


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

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


1086
2017-11-29 03:00


起源



答案:


首先,我假设您有一个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();

14
2017-11-29 05:23



答案很棒。最后我做了类似的事情:CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),[UIColor clearColor] .CGColor); - MB.
然后[路径中风] - MB.


答案:


首先,我假设您有一个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();

14
2017-11-29 05:23



答案很棒。最后我做了类似的事情:CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),[UIColor clearColor] .CGColor); - MB.
然后[路径中风] - MB.