【发布时间】:2012-07-15 05:54:44
【问题描述】:
我正在尝试叠加 2 张图片。底部图像从图像库加载到一个 alpha 设置为 1.0 的 UIImageView 对象。此图像中的所有像素的 alpha 值也为 1.0。我在上下文中创建的第二个图像。此图像的 alpha 值因像素而异。在上下文中创建第二个图像后,我将其绘制到显示在第一个 UIImageView 顶部的第二个 UIImageView。这第二个 UIImageView 也有一个设置为 1.0 的 alpha。两个 UIImageView 的背景属性都设置为 clear。
我试图在运行时使用滑块更改第二张图像中像素的单个 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);
【问题讨论】:
-
你不能把一个图像视图放在另一个上面,然后将最上面的图像视图的
opaque属性设置为NO吗? -
试过了,但它不会改变结果。感谢您的回复!
标签: objective-c ios xcode uiimage blend