简体   繁体   中英

How to create a UIImage with a transparent background

I am dynamically creating a (drawing my own custom) UIImage for the 'UIImageView' property on a UITableViewCell.

When a user selects the tableView cell, the cell background turns blue but my custom UIImage background does not - it remains white (so it is painfully clear I've got an image sitting there). I've tried various drawing approaches

// 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);

but in both of these approaches, the background is black. The only thing that seems to work is to finishing creating the image and then apply create a 2nd image with 'CGImageCreateWithMaskingColors' based on the first image - masking out the background.

Is there a way I can "paint" a transparent background in the first place?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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