帮助对由图像蒙版/边界定义的图层进行着色并将图像覆盖在顶部

发布于 2024-11-28 05:59:15 字数 1531 浏览 2 评论 0原文

我目前正在为客户试验一款头像创建 iOS 应用程序,在某些时候用户应该可以选择更改项目的颜色。

客户端像往常一样提供要以 .png 格式着色的图库项目,并具有不同的 alpha/不透明度值,以有效地生成阴影。然而,项目着色的正确最终结果不是遮盖图像并应用颜色混合,因为透明区域保持原样,而是“填充”由项目的“外部描边”包围的背景区域,然后在项目的顶部重新绘制图像。这样阴影就会应用到新颜色上。

这是我所取得的成就:

UIImage *image =  item.image;
UIGraphicsBeginImageContext(image.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -area.size.height);

CGContextSaveGState(context);

// This applies the colorization only on the image mask 
//CGContextClipToMask(context, area, image.CGImage);

[[UIColor redColor] set];
CGContextFillRect(context, area);

CGContextRestoreGState(context);

CGContextSetBlendMode(context, kCGBlendModeNormal);

CGContextDrawImage(context, area, image.CGImage);

UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

item.image = coloredImg;

产生以下结果。

http://dl.dropbox.com/u/1237004/Screen%20shot%202011-07-15%20at%207.53.50%20%CE%BC.%CE%BC..png< /a>

这基本上接近我真正想要实现的目标,除了红色区域扩展到裤子之外。

那么怎样才能消除这部分呢?我正在考虑将 alpha 值减少到未由图像蒙版或类似内容定义的区域,但无法接近解决方案。

谢谢大家的建议。

注意:屏幕截图中显示的项目是 WeeMee 头像创建应用程序的一部分,未在我们的应用程序中使用,而是我的实验的一部分,所有权利均属于其各自所有者

I am currently experimenting with an avatar creation iOS app for a client, where at some point the user should have the option of changing the color of an item.

The client provides the gallery items to be colorized in .png format with different alpha/opacity values as usual, to effectively produce the shades. The correct final result of the item colorization however is not masking the image and applying a color blend since transparent areas remain as is, but rather "fill" the background area enclosed by the item's "outer stroke" and then redraw the image on top of it so the shades apply on the new color.

Here's what I've achieved:

UIImage *image =  item.image;
UIGraphicsBeginImageContext(image.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -area.size.height);

CGContextSaveGState(context);

// This applies the colorization only on the image mask 
//CGContextClipToMask(context, area, image.CGImage);

[[UIColor redColor] set];
CGContextFillRect(context, area);

CGContextRestoreGState(context);

CGContextSetBlendMode(context, kCGBlendModeNormal);

CGContextDrawImage(context, area, image.CGImage);

UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

item.image = coloredImg;

which produces the following result.

http://dl.dropbox.com/u/1237004/Screen%20shot%202011-07-15%20at%207.53.50%20%CE%BC.%CE%BC..png

This is basically close to what I really want to achieve, except the red area expanding outside the pants.

So how's possible to eliminate that part? I was thinking of maybe reducing the alpha value to the area NOT defined by the image mask or something like that but couldn't come close to a solution.

Thank you all for your suggestions.

NOTE: the item showing in the screenshot is part of the WeeMee avatar creation application, not used in our app but rather part of my experiments, and all rights belong to it's respective owners

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

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

发布评论

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

评论(1

似梦非梦 2024-12-05 05:59:15

你需要做的是设置一个剪切路径,并指示CG仅填充内部区域。如果您已经绘制了定义裤子的路径,那么这对您来说应该是微不足道的。

请参阅本教程:
教程

更新以响应评论:

屏蔽图像

What you need to do is to set up a clipping path, and instruct CG to fill only inside the inside area. If you're already drawing the path that define the pants, this should be trivial for you.

See this tutorial:
tutorial

Updated in response to comments:

Masking an image

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