如何创建一个带有透明背景的UIImage

5
我正在为UITableViewCell的“UIImageView”属性动态创建(绘制自定义)UIImage。当用户选择tableView单元格时,单元格背景变蓝,但我的自定义UIImage背景不会改变-它仍然是白色(因此非常清楚我有一个图像在那里)。我尝试了各种绘图方法。
// when I create the cell
cell.imageView.backgroundColor = [UIColor clearColor];
cell.imageView.opaque = NO;

// 1. when I create the UIImage - clearColor background
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);

// 2. when I create the UIImage - clear background
CGContextClearRect(context, rect);

但是在这两种方法中,背景都是黑色的。唯一有效的方法似乎是先完成图像的创建,然后基于第一个图像使用“CGImageCreateWithMaskingColors”创建第二个图像 - 掩盖掉背景。

有没有办法一开始就“绘制”透明背景呢?


1
你是否在使用 UIGraphicsBeginImageContextWithOptions 来设置图像上下文?你是否将 opaque 参数设为 NO - rmaddy
1个回答

11

在设置图像上下文时,请确保将opaque参数传递为NO,并使用UIGraphicsBeginImageContextWithOptions函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接