[英]CALayer - How do I create an image with a solid background and transparent text?
我正在嘗試使用AVComposition創建電影生成應用程序,並且在制作標題框架時遇到麻煩。 每個幀實際上是一個calayer,標題層位於其他幀的頂部。
標題(文本)需要透明,黑色背景,以便他們可以在標題文本字母下看到第一個內容框架的某些部分。
我搜索了大部分有關calayer面具的文章,但沒有任何幫助我。 我認為這篇文章( 如何只在IOS中的UIView中使文本/標題覆蓋的部分透明 )是有用的,並且編碼像Dave的方式,但只有白屏。
這是我做的:
// create UILabel from the title text
CGRect rectFrame = CGRectMake(0, 0, videoSize.width, videoSize.height);
UILabel *lbTitle = [[UILabel alloc] initWithFrame:rectFrame];
lbTitle.text = self.titleText;
lbTitle.font = [UIFont fontWithName:@"Helvetica" size:60];
lbTitle.textColor = [UIColor blackColor];
lbTitle.backgroundColor = [UIColor whiteColor];
// get title image and create mask layer
UIGraphicsBeginImageContextWithOptions(lbTitle.bounds.size, TRUE, [[UIScreen mainScreen] scale]);
[lbTitle.layer renderInContext:UIGraphicsGetCurrentContext()];
CGImageRef viewImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
UIGraphicsEndImageContext();
CALayer *maskLayer = [CALayer layer];
maskLayer.contents = (__bridge id)viewImage;
maskLayer.frame = rectFrame;
// create title background layer and set mastLayer as mast layer of this layer
// this layer corresponds to "UIView's layer" in Dave's method
CALayer *animatedTitleLayer = [CALayer layer];
animatedTitleLayer.backgroundColor = [UIColor whiteColor].CGColor;
animatedTitleLayer.mask = maskLayer;
animatedTitleLayer.frame = rectFrame;
...
[view.layer addSubLayer:animatedTitleLayer];
這里我使用animatedTitleLayer作為標題背景(黑色背景),但我看到的是白色屏幕。
有人可以幫幫我嗎? 提前致謝。
掩碼使用alpha通道來確定要屏蔽的部分以及要保留的部分。 但是,渲染到圖像中的標簽在白色背景上呈現為黑色文本,因此圖像中沒有透明度。
您還指定了用於渲染圖像的圖形上下文是不透明的,因此即使標簽的背景顏色很清晰,您也會得到不透明的圖像。
因此,您需要在標簽上設置清晰的背景顏色,並在創建圖形上下文時將NO
作為第二個參數傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.