将颜色烧伤限制在 CGPath 内部

发布于 2024-12-28 04:10:24 字数 2911 浏览 2 评论 0原文

我有一个定义为 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

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

I have a closed path defined as a CGPath (actually, CGMutablePath).

I want to color burn a specific region of a UIImage defined by the path. I've been fiddling around with Core Graphics, but currently my only options are to color tint a rectangular area defined by a CGRect, not a path.

Can anyone point me in the right direction?

--Update

Using the help of Rob I managed to rotate the image I got from the camera 90 deg to let it appear correctly in the UIImageview..

Only problem I have left is the PATH i need to draw is still 90 degrees of, and out of scale. The code i currently use is as following:

- (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;
}

The following image is the result. De red rectangle is a representation of the CGPATH. The white square is actually the result of the above method on the path.

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

I suppose I need to manually rotate the CGPATH 90 degrees using COS math's and what not.. Although I might oversee something.. ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-04 04:10:24

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

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

Set the context's clipping path to your path to constrain the affected pixels.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文