2014-04-16 4 views

답변

6

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(); 
+0

감사를 절단 할 필요가 터치 영역을 필요로 할 어떤 경로를 정의 할 수 있습니다. 그것은 나를 많이 돕는다. – user2951348

1

이 하나

먼저 quartzcore를 추가하십시오

#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; 
관련 문제