iOS

最近更新时间:2026-02-02 11:43:41

我的收藏
本文档将介绍在 TRTC SDK 项目中如何集成和使用 TEBeautyKit 库Demo 可参考 TRTC_Adapter_Example

集成步骤

1. (可选)集成 TRTCAdapter。TRTCAdapter.framework 用来接收 TRTC SDK 的视频回调,内部经过美颜 SDK 处理后再返回给 TRTC SDK,如需对视频流做自定义处理,可以跳过此步骤。



2. 集成 TEBeautyKit,编辑 podfile 文件,添加下面的代码后执行 pod install
# 将S1-07替换成您购买的套餐
pod 'TEBeautyKit/S1-07', :podspec => 'https://mediacloud-76607.gzc.vod.tencent-cloud.com/TencentEffect/iOS/TEBeautyKit/latest/TEBeautyKit.podspec'
3. 集成 面板资源,点击下载资源包后,引入主工程,内容说明见 附录
4. 集成 美颜素材

使用流程

步骤一:鉴权

使用 SDK 前,需要进行一次美颜鉴权,鉴权成功后才能正常使用美颜功能。鉴权出错可参见 鉴权错误码 说明。
[TEBeautyKit setTELicense:@"your license" key:@"your key" completion:^(NSInteger authresult, NSString * _Nullable errorMsg) {
NSLog(@"----------result: %zd %@",authresult,errorMsg);
}];

步骤二:配置面板资源路径

美颜面板上面的美颜数据、icon 图标都在 面板资源 中,根据接口说明,传入对应的 json 文件路径,面板上显示的内容可以根据实际需求自行调整。
- (void)configPanel {
NSBundle *bundle = [NSBundle mainBundle];
NSString *beautyJsonPath = [bundle pathForResource:@"beauty" ofType:@"json"]; //美颜
NSString *lutJsonPath = [bundle pathForResource:@"lut" ofType:@"json"]; //滤镜
NSString *motion2dJsonPath = [bundle pathForResource:@"motion_2d" ofType:@"json"]; //2d贴纸
NSMutableArray *resArray = [[NSMutableArray alloc] init];
[resArray addObject:@{TEUI_BEAUTY : beautyJsonPath}];
[resArray addObject:@{TEUI_LUT : lutJsonPath}];
[resArray addObject:@{TEUI_MOTION_2D : motion2dJsonPath}];
/// 设置资源
[[TEUIConfig shareInstance] setTEPanelViewResources:resArray];
}

步骤三:添加面板

TEPanelView 是自定义的面板视图,用来展示步骤二中配置的数据。
- (void)addPanelView {
TEPanelView *tePanelView = [[TEPanelView alloc] init];
tePanelView.delegate = self;
[self.view addSubview:tePanelView];
[tePanelView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.bottom.mas_equalTo(self.view);
make.left.right.mas_equalTo(self.view);
make.height.mas_equalTo(230 + self.view.safeAreaInsets.bottom);
}];
}
如未集成 TRTCAdapter,请直接参考 步骤七

步骤四:adapter 绑定美颜

/// 创建adapter对象
- (TEBeautyTRTCAdapter *)trtcAdapter {
if (!_trtcAdapter) {
_trtcAdapter = [[TEBeautyTRTCAdapter alloc] init];
}
return _trtcAdapter;
}
/// 绑定美颜
__weak __typeof(self)weakSelf = self;
[self.trtcAdapter bind:self.trtcCloud onCreatedXmaicApi:^(XMagic * _Nullable xmagicApi) {
__strong typeof(self) strongSelf = weakSelf;
strongSelf.teBeautyKit.xmagicApi = xmagicApi;
[strongSelf.teBeautyKit setLogLevel:YT_SDK_ERROR_LEVEL];
strongSelf.tePanelView.teBeautyKit = strongSelf.teBeautyKit;
[strongSelf.tePanelView setDefaultBeauty];
} onDestroyXmaicApi:^{
__strong typeof(self) strongSelf = weakSelf;
[strongSelf.teBeautyKit onDestroy];
strongSelf.teBeautyKit = nil;
}];

步骤五:参数变化通知 adapter

/// 通知adapter前后置摄像头,是否编码镜像
[self.trtcAdapter notifyCameraChanged:self.isFrontCamera isEncoderMirror:self.isEncoderMirror];
/// 通知adapter屏幕方向改变
[self.trtcAdapter setDeviceOrientation:currOrientation];

步骤六:解绑 adapter,销毁美颜

[self.trtcAdapter unbind];
self.trtcAdapter = nil;

步骤七:未集成 TRTCAdapter

监听 TRTC 视频回调,在回调中处理美颜,然后返回给 TRTC。
1. 初始化。
- (void)initXMagic {
__weak __typeof(self)weakSelf = self;
[TEBeautyKit createXMagic:EFFECT_MODE_PRO onInitListener:^(TEBeautyKit * _Nullable beautyKit) {
__strong typeof(self)strongSelf = weakSelf;
strongSelf.teBeautyKit = beautyKit;
strongSelf.tePanelView.teBeautyKit = strongSelf.teBeautyKit;
[strongSelf.tePanelView setDefaultBeauty];
[strongSelf.teBeautyKit setLogLevel:YT_SDK_ERROR_LEVEL];
[strongSelf.teBeautyKit registerSDKEventListener:strongSelf];
}];
}
2. 处理视频数据。
- (uint32_t)onProcessVideoFrame:(TRTCVideoFrame *)srcFrame dstFrame:(TRTCVideoFrame *)dstFrame {
YTProcessOutput *output = [self.teBeautyKit processTexture:srcFrame.textureId
textureWidth:srcFrame.width
textureHeight:srcFrame.height
withOrigin:YtLightImageOriginTopLeft
withOrientation:YtLightCameraRotation0];
dstFrame.textureId = output.textureData.texture;
return 0;
}
3. 如需销毁,可参考如下:
- (void)destroyXMagic {
[self.teBeautyKit onDestroy];
self.teBeautyKit = nil;
}