集成准备
- 下载并解压 Demo 包,将 Demo 工程中的 xmagic 模块(bundle,XmagicIconRes 两个文件夹下面的文件,Record > View 文件夹下面的文件)导入到实际项目工程中。
- 导入 lib 目录中的
libpag.framework
、Masonry.framework
、XMagic.framework
和YTCommonXMagic.framework
。 - framework 签名 General--> Masonry.framework 和 libpag.framework 选 Embed & Sign。
- 将 Bundle ID 修改成与申请的测试授权一致。
SDK 接口集成
- 步骤一 和 步骤二 可参考 Demo 工程中,UGCKitRecordViewController 类 viewDidLoad,buildBeautySDK 方法。
- 步骤四 至 步骤七 可参考 Demo 工程的 UGCKitRecordViewController,BeautyView 类相关实例代码。
步骤一:初始化授权
- 在工程 AppDelegate 的 didFinishLaunchingWithOptions 中添加如下代码,其中 LicenseURL,LicenseKey 为腾讯云官网申请到授权信息,请参见 License 指引:
[TXUGCBase setLicenceURL:LicenseURL key:LicenseKey]; [TELicenseCheck setTELicense:@"https://license.vod2.myqcloud.com/license/v2/1258289294_1/v_cube.license" key:@"3c16909893f53b9600bc63941162cea3" completion:^(NSInteger authresult, NSString * _Nonnull errorMsg) { if (authresult == TELicenseCheckOk) { NSLog(@"鉴权成功"); } else { NSLog(@"鉴权失败"); } }];
- 授权代码可参考 Demo 中 UGCKitRecordViewController 类 viewDidLoad 中的授权代码:
NSString *licenseInfo = [TXUGCBase getLicenceInfo]; NSData *jsonData = [licenseInfo dataUsingEncoding:NSUTF8StringEncoding]; NSError *err = nil; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; NSString *xmagicLicBase64Str = [dic objectForKey:@"TELicense"]; //初始化 xmagic 授权 int authRet = [XMagicAuthManager initAuthByString:xmagicLicBase64Str withSecretKey:@""];// withSecretKey 为空字符串, 不需要填写内容 NSLog(@"xmagic auth ret : %i", authRet); NSLog(@"xmagic auth version : %@", [XMagicAuthManager getVersion]);
步骤二:设置 SDK 素材资源路径
CGSize previewSize = [self getPreviewSizeByResolution:self.currentPreviewResolution];
NSString *beautyConfigPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
beautyConfigPath = [beautyConfigPath stringByAppendingPathComponent:@"beauty_config.json"];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
BOOL isDir = YES;
NSDictionary * beautyConfigJson = @{};
if ([localFileManager fileExistsAtPath:beautyConfigPath isDirectory:&isDir] && !isDir) {
NSString *beautyConfigJsonStr = [NSString stringWithContentsOfFile:beautyConfigPath encoding:NSUTF8StringEncoding error:nil];
NSError *jsonError;
NSData *objectData = [beautyConfigJsonStr dataUsingEncoding:NSUTF8StringEncoding];
beautyConfigJson = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
}
NSDictionary *assetsDict = @{@"core_name":@"LightCore.bundle",
@"root_path":[[NSBundle mainBundle] bundlePath],
@"tnn_"
@"beauty_config":beautyConfigJson
};
// Init beauty kit
self.beautyKit = [[XMagic alloc] initWithRenderSize:previewSize assetsDict:assetsDict];
步骤三:添加日志和事件监听
// Register log
[self.beautyKit registerSDKEventListener:self];
[self.beautyKit registerLoggerListener:self withDefaultLevel:YT_SDK_ERROR_LEVEL];
步骤四:配置美颜各种效果
- (int)configPropertyWithType:(NSString *_Nonnull)propertyType withName:(NSString *_Nonnull)propertyName withData:(NSString*_Nonnull)propertyValue withExtraInfo:(id _Nullable)extraInfo;
步骤五:进行渲染处理
在短视频预处理帧回调接口,构造 YTProcessInput 将 textureId 传入到 SDK 内做渲染处理。
[self.xMagicKit process:inputCPU withOrigin:YtLightImageOriginTopLeft withOrientation:YtLightCameraRotation0]
步骤六:暂停/恢复 SDK
[self.beautyKit onPause];
[self.beautyKit onResume];
步骤七:布局中添加 SDK 美颜面板
UIEdgeInsets gSafeInset;
#if __IPHONE_11_0 && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
if(gSafeInset.bottom > 0){
}
if (@available(iOS 11.0, *)) {
gSafeInset = [UIApplication sharedApplication].keyWindow.safeAreaInsets;
} else
#endif
{
gSafeInset = UIEdgeInsetsZero;
}
dispatch_async(dispatch_get_main_queue(), ^{
//美颜选项界面
_vBeauty = [[BeautyView alloc] init];
[self.view addSubview:_vBeauty];
[_vBeauty mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(self.view);
make.centerX.mas_equalTo(self.view);
make.height.mas_equalTo(254);
if(gSafeInset.bottom > 0.0){ // 适配全面屏
make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(0);
} else {
make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-10);
}
}];
_vBeauty.hidden = YES;
});