2013-06-21 38 views
0

我試圖讓我的圖像在底部的角落透明,以支持臉部彎曲的外觀。我試着用下面的代碼,但CGPathAddArc是有點混亂,因爲我不是一個數學虛張聲勢。 在圖像中顯示紅色角落定義我要讓透明UIImage底部的透明像素?

What I have

Red line shape I want to make transparent

UIGraphicsBeginImageContext(originalImage.size); 
[originalImage drawAtPoint:CGPointZero]; 

CGMutablePathRef testRef = CGPathCreateMutable(); 
//CGAffineTransform temp = CGAffineTransformMakeRotation(M_PI/2); 
//CGPathAddEllipseInRect(testRef, &temp, CGRectMake(0,0,20,20)); 
//CGPathAddQuadCurveToPoint(testRef, nil, 20,20, 150, 150); 
//CGPathAddCurveToPoint(testRef, NULL, 20, 20, 40, 40, 100, 100); 

CGPathMoveToPoint(testRef, NULL, 20, 20); 
CGPathAddArc(testRef, NULL, 0, 15, 15, M_PI_2, -M_PI_2, true); 

// Clip to the path and clear that portion of the image. 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextAddPath(context,testRef); 
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

如果您不熟悉的核心基礎和低層次的東西,你可以簡單的區域使用[UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:]來獲得帶圓角的矩形。

實施例:

UIGraphicsBeginImageContext(originalImage.size); 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height) 
               byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight 
                cornerRadii:CGSizeMake(15, 15)]; 
    [path addClip]; 
    [originalImage drawAtPoint:CGPointZero]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();