如何创建具有透明背景的UIImage

Lut*_*ker 5 uitableview uiimage cgcontext ios

我正在为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);
Run Code Online (Sandbox Code Playgroud)

但在这两种方法中,背景都是黑色的.唯一可行的方法是完成创建图像,然后应用基于第一个图像的'CGImageCreateWithMaskingColors'创建第二个图像 - 屏蔽背景.

我有没有办法首先"绘制"透明背景?

rma*_*ddy 9

设置图像上下文时,请确保传递NO给函数的opaque参数UIGraphicsBeginImageContextWithOptions.