簡體   English   中英

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

[英]How to create a UIImage with a transparent background

我正在為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'創建第二個圖像 - 屏蔽背景。

我有沒有辦法首先“繪制”透明背景?

在設置圖像上下文時,請確保將NO傳遞給UIGraphicsBeginImageContextWithOptions函數的opaque參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM