简体   繁体   English

在UIImage的边缘上进行Alpha混合

[英]Alpha blending on the edges of an UIImage

I have two rectangular images: one is foreground, the other one is background. 我有两个矩形图像:一个是前景,另一个是背景。 I am trying to blend the edges of the foreground image so that the foreground image looks like it is "part" of the background image. 我正在尝试混合前景图像的边缘,以使前景图像看起来像是背景图像的“一部分”。 In other words, I am trying to apply a transparency effect, with the opacity of the foreground image decreasing from 100% at the center of the image down to 0% at the edges. 换句话说,我正在尝试应用透明效果,使前景图像的不透明度从图像中心的100%降低到边缘的0%。 I have found that this operation is sometimes referred by different names, such as: alpha compositing, alpha blending, edge feathering or edge transparency. 我发现有时会用不同的名称来引用此操作,例如:Alpha合成,Alpha混合,边缘羽化或边缘透明度。 Here is a more detailed description of the effect I am trying to obtain: http://en.wikipedia.org/wiki/Alpha_compositing 这是我要获得的效果的更详细说明: http : //en.wikipedia.org/wiki/Alpha_compositing

I have looked at the CGContext documentation, but I haven't found any function that would do that out of the box. 我看过CGContext文档,但没有发现任何能立即使用的功能。

Is there any way to do that using CGContext or even OpenGL? 有什么方法可以使用CGContext甚至是OpenGL? Would there be a way to do it on a non-rectangular image? 是否可以在非矩形图像上进行处理? I know, all images are rectangular, but I mean an image with let's say, a circle in it, and a transparent area all around. 我知道,所有图像都是矩形的,但是我的意思是一个图像,其中有一个圆形,周围有一个透明区域。

The CGImageCreateWithMask() function in the iOS library can do alpha blending with decreasing opacity on the edges. iOS库中的CGImageCreateWithMask()函数可以进行alpha混合,同时减少边缘的不透明度。 It's possible to get something to work by combining the code in these two links: 通过将以下两个链接中的代码组合在一起,可以使某些工作起作用:

http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html

Creating mask with CGImageMaskCreate is all black (iphone) 用CGImageMaskCreate创建蒙版全黑(iPhone)

The level of gray (from black to white) of each pixel in the mask will define the level of opacity during blending. 蒙版中每个像素的灰度级别(从黑色到白色)将定义混合期间的不透明度级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM