我有一个UIImage,其中部分内容被用户选中以清除(make transparent)。为了进行选择,我使用了NSBezierPath。
如何在iOS中清除/制作UIImage的透明部分?
答案 0 :(得分: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();
答案 1 :(得分:0)
Swift4解决方案:
UIGraphicsBeginImageContext(image!.size)
image!.draw(at: CGPoint.zero)
let context:CGContext = UIGraphicsGetCurrentContext()!;
let bez = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 10, height: 10))
context.addPath(bez.cgPath)
context.clip();
context.clear(CGRect(x: 0,y: 0,width: image!.size.width,height: image!.size.height));
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!;
UIGraphicsEndImageContext();