我试图覆盖2个图像。将底部图像从图像库加载到Alpha设置为1.0的UIImageView对象中。此图像中的所有像素也具有1.0的alpha值。我在上下文中创建的第二个图像。该图像的alpha值因像素而异。在上下文中创建第二个图像后,我将其绘制到第一个UIImageView顶部显示的第二个UIImageView。这第二个UIImageView也有一个alpha设置为1.0。这两个UIImageView都有背景属性设置为清除。UIImage混合模式问题
我试图在运行时使用滑块更改第二个图像中像素的单独alpha值,但是,我无法获得第二个图像的RGB值以便与第一个图像正确混合。我不想基于亮度,黑暗度等进行混合。似乎我想要kCGBlendModeCopy,但这似乎仍然基于明度或其他参数进行混合。
为了帮助可视化问题:想象一下,我的底部UIImageView是一个人的照片。我的顶级UIImageView由2个坚实的分隔圆圈组成; 1个红色和1个绿色。使用滑块,如果我将红色像素的Alpha值从0.0更改为1.0,然后红色圆圈应淡入为纯红色,并且淡出完全平稳。
我可以通过将RGB数据数组值设置为等于alpha值并将alpha值设置为1.0来验证我的alpha值是否正常工作。这显示出一个黑色的圆圈逐渐消失在白色和背面。
下面是我用来创建第一张图像的代码。
//image <-- is the name of the second UIImageView that's passed to this method
//rgbaDataNew <-- is the name of my pixel data array for the second image
CGContextRef context;
CGImageRef imageRef;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(rgbaDataNew, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
imageRef = CGBitmapContextCreateImage (context);
UIImage * topImage = [UIImage imageWithCGImage:imageRef];
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [topImage CGImage]);
image.image = [UIImage imageWithCGImage:CGBitmapContextCreateImage (context)];
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CGImageRelease(imageRef);
难道你不能只将一个图像视图放在另一个图像视图上,并将t opmost图像视图的'opaque'属性为'NO'? – jrturton 2012-07-15 06:13:09
试过了,但没有改变结果。感谢您的回应! – 2012-07-15 06:19:52