将颜色烧伤限制在CGPath的内部

时间:2012-01-20 20:40:08

标签: ios uiimage core-graphics cgpath tint

我有一个被定义为CGPath的闭路径(实际上是CGMutablePath)。

我想为路径定义的UIImage的特定区域着色。我一直在使用Core Graphics,但目前我唯一的选择是为CGRect定义的矩形区域着色,而不是路径。

有人能指出我正确的方向吗?

- 更新

在Rob的帮助下,我设法将我从相机获得的图像旋转90度,让它在UIImageview中正确显示..

我唯一留下的问题是我需要绘制的PATH仍然是90度,并且超出规模。我目前使用的代码如下:

- (UIImage*)applyColorFillWithPath:(CGMutablePathRef) path withColor:(UIColor*)color {

CGFloat targetWidth = self.size.width;
CGFloat targetHeight = self.size.height;
CGImageRef imageRef = [self CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

if (bitmapInfo == kCGImageAlphaNone) {
    bitmapInfo = kCGImageAlphaNoneSkipLast;
}

CGContextRef context;
if (self.imageOrientation == UIImageOrientationUp || self.imageOrientation == UIImageOrientationDown) {
    //UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    context = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

} else {
    //UIGraphicsBeginImageContext(CGSizeMake(targetHeight, targetWidth));
    context = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

}   

// set the fill color
//[color setFill];

if (self.imageOrientation == UIImageOrientationLeft) {
    CGContextRotateCTM(context, radians(90));
    CGContextTranslateCTM (context, 0, -targetHeight);

} else if (self.imageOrientation == UIImageOrientationRight) {
    CGContextRotateCTM (context, radians(-90));
    CGContextTranslateCTM (context, -targetWidth, 0);

} else if (self.imageOrientation == UIImageOrientationUp) {
    // NOTHING
} else if (self.imageOrientation == UIImageOrientationDown) {
    CGContextTranslateCTM (context, targetWidth, targetHeight);
    CGContextRotateCTM (context, radians(-180));
}

CGContextDrawImage(context, CGRectMake(0, 0, targetWidth, targetHeight),imageRef);

CGContextBeginPath(context);
CGContextAddPath(context, path);
CGContextSaveGState(context);
CGContextClip(context);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, CGPathGetBoundingBox(path));
CGContextRestoreGState(context);

// Turn the bitmap context into a UIImage.
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *coloredImg = [UIImage imageWithCGImage:cgImage scale:1 orientation:UIImageOrientationUp];
CGImageRelease(cgImage);

//return the color-burned image
return coloredImg;
}

结果如下图所示。 De red rectangle是CGPATH的表示。白色方块实际上是上述方法在路径上的结果。

http://i41.tinypic.com/f1zbdi.png

我想我需要使用COS数学手动旋转CGPATH 90度而不是..虽然我可能会监督某些事情......?

1 个答案:

答案 0 :(得分:2)

将上下文的剪切路径设置为路径,以约束受影响的像素。

CGContextRef gc = myGraphicsContext();
CGMutablePathRef path = myMutablePathRef();

CGContextBeginPath(gc);
CGContextAddPath(gc, path);
CGContextSaveGState(gc); {
    CGContextClip(gc);

    // Do color burn.  For example:
    CGContextSetBlendMode(gc, kCGBlendModeColorBurn);
    CGContextSetFillColorWithColor(gc, [UIColor redColor].CGColor);
    CGContextFillRect(gc, CGPathGetBoundingBox(path));
} CGContextRestoreGState(gc);