3

我正在尝试使用 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作为标题背景(黑色背景),但我看到的是白屏。

任何人都可以帮助我吗?提前致谢。

4

1 回答 1

1

蒙版使用 Alpha 通道来确定要屏蔽哪些部分以及要保留哪些部分。但是,您渲染到图像中的标签被渲染为白色背景上的黑色文本,因此图像中没有透明度。

您还指定用于渲染图像的图形上下文是不透明的,因此即使标签的背景颜色清晰,您也会得到不透明的图像。

因此,您需要在标签上设置清晰的背景颜色,并NO在创建图形上下文时作为第二个参数传递。

于 2014-03-13T07:21:16.963 回答