[英]ios - How to cut a hole in an UIImage
有什么方法可以在我的圖像上打一個洞。 我想要的是割眼睛和嘴。 我希望它變得透明。
制作一個UIBezierPath將要使其透明的閉合路徑的所有頂點。 貝塞爾曲線的路徑如下所示:
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];
然后使用以下代碼使該區域透明並保存為新圖像
// 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();
試試這個
首先添加石英芯
#import <QuartzCore/QuartzCore.h>
創建您要剪切的路徑
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:yourView.bounds];
[maskPath appendPath:[UIBezierPath bezierPathWithRect:rectToPunchThroughTheView]];
然后創建一個遮罩層並將其添加到視圖中-CAShapeLayer將允許您定義任何路徑
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.fillColor = [UIColor blackColor].CGColor;
maskLayer.path = maskPath.CGPath;
yourView.layer.mask = maskLayer;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.