【问题标题】:Constrain color burn to the inside of a CGPath将颜色燃烧限制在 CGPath 的内部
【发布时间】:2012-02-15 09:12:12
【问题描述】:

我有一个封闭路径定义为 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;
}

下图是结果。红色矩形是 CGPATH 的表示。白色方块其实就是上面的方法在路径上的结果。

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

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

【问题讨论】:

    标签: ios uiimage core-graphics cgpath tint


    【解决方案1】:

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

    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);
    

    【讨论】:

    • 谢谢罗伯!你真的帮助了我。基本上我觉得它太难了..尝试创建一个蒙版,复制内容,着色并将其合并回源图片......
    • 我剩下的唯一奇怪的事情是包含图像的图像视图在应用颜色燃烧时将图像旋转了 90 度。看起来它会自动旋转到风景或其他东西。 UIImage 是从图像选择器中以纵向模式拍摄的照片。我已将 Imageview 模式设置为“缩放填充”。
    • 谢谢(再次)Rob...我确实搜索过类似的问题,但我一直在错误地绘制 CGPath,例如偏离 90 度。我根据您的输入重新陈述了我原来的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多