如何在iPhone SDK中为UIImageView中的区域着色?

时间:2014-02-03 12:49:24

标签: ios iphone uiview uiimageview quartz-core

我遇到一个问题,我需要在UIImageView's图片Please look the image attached

中为不均匀区域着色

让我知道解决问题的方法和合适的例子。 我会提前感谢你。

我做了一项研究,说我需要使用下面的代码来使用UIView子类。

- (void)drawRect:(CGRect)rect

{     float polySize = 60; //改变这个

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

CGAffineTransform t0 = CGContextGetCTM(context);
t0 = CGAffineTransformInvert(t0);
CGContextConcatCTM(context, t0);

//Begin drawing setup
CGContextBeginPath(context);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetLineWidth(context, 2.0);

CGPoint center;

//Start drawing polygon
center = CGPointMake(160, 90.0);
CGContextMoveToPoint(context, center.x, center.y + polySize);
for(int i = 1; i < 6; ++i)
{
    CGFloat x = polySize * sinf(i * 2.0 * M_PI / 6);
    CGFloat y = polySize * cosf(i * 2.0 * M_PI / 6);
    CGContextAddLineToPoint(context, center.x + x, center.y + y);
}

//Finish Drawing
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathStroke);
CGContextRestoreGState(context);

}

1 个答案:

答案 0 :(得分:0)

您应该可以使用Core Image执行此操作。

由于您想要更改的图像部分是固定的,因此您可以创建一个遮罩来定义要更改的图像部分。

然后,您可以使用CIMaskToAlpha来屏蔽除要更改的部分之外的所有内容,然后CIHueAdjust更改未屏蔽部分的色调。然后,您将图像的调整部分重叠在顶部。

或者,您可以创建一个在您想要更改的区域中为纯色的图像,并在其他任何位置创建透明图像,然后使用CIHueBlendMode过滤器将彩色区域的色调一次性应用于目标图像