提交工单
咨询集成、功能及报价等问题
在实现基本的 Avatar 功能之前,请确保:
请先在 ZEGO 控制台 创建项目,并申请有效的 AppID 和 AppSign,详情请参考 控制台 - 项目管理 中的“项目信息”。
请联系 ZEGO 商务人员,提供自己项目的包名(Bundle Identifier),开通相关权限。
ZEGO 提供了一个创建虚拟形象的最简示例代码,可作为开发中的参考。
//
// ViewController.m
//
//
#import "ViewController.h"
#import <ZegoAvatar/ZegoAvatarService.h>
@interface ViewController () <ZegoAvatarServiceDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initSDK];
}
- (void)initSDK {
// AI 模型路径
NSString *AIPath = [NSString stringWithFormat:@"%@%@", [NSBundle mainBundle].resourcePath, @"/ios/AIModel.bundle"];
ZegoServiceConfigV2 *config = [[ZegoServiceConfigV2 alloc] init];
// 请通过 ZEGO 控制台获取,格式为:1234567890
config.appID = <#appID#>;
// 请通过 ZEGO 控制台获取,格式为:@"0123456789012345678901234567890123456789012345678901234567890123"(共64个字符)
config.appSign = @"<#appSign#>";
config.AIPath = AIPath;
// 设置 ZegoAvatarService 代理
[[ZegoAvatarService sharedInstance] addServiceObserver:self];
// 初始化 ZegoAvatarService
[[ZegoAvatarService sharedInstance] initWithConfigV2:config];
}
- (void)onStateChange:(ZegoAvatarServiceState)state {
// SDK 初始化成功
if (state == ZegoAvatarServiceStateInitSucceed) {
[self setupUI];
}
}
- (void)setupUI {
// 创建 IZegoAvatarView 实例对象(avatarView)
id<IZegoAvatarView> avatarView = [[ZegoAvatarService sharedInstance] createAvatarView:self.view.bounds];
[self.view addSubview:(UIView *)avatarView];
// 传入模型路径,以 Q 版男生模型为例,Q 版女生模型请使用 Girl.prefab
NSString *resourcePath = [NSBundle mainBundle].resourcePath;
NSString *characterPath = [NSString stringWithFormat:@"%@%@", resourcePath, @"/assets/qhuman.bundle/AssetBundles/bundle/qbody/Boy.prefab"];
// 创建 IZegoCharacter 实例对象(character)
id<IZegoCharacter> character = [[ZegoAvatarService sharedInstance] createCharacter:characterPath];
// 虚拟形象上屏,将 character 添加到 avatarView
[avatarView setCharacter:character];
}
@end
如需了解最简代码各步骤说明,请参考 实现流程。
本节介绍如何使用 ZegoAvatar SDK 实现基本的图像处理功能,API 调用时序如下图:
请确认您项目的 bundle id
是否为申请权限时所提供的包名。
引入头文件,准备基础工作。
#import <ZegoAvatar/ZegoAvatarService.h>
调用 addServiceObserver 接口设置 ZegoAvatarServiceDelegate 代理。注册 onStateChange 回调,接收初始化状态的相关回调通知。
// 设置 ZegoAvatarServiceDelegate 代理
[[ZegoAvatarService sharedInstance] addServiceObserver:self];
// 实现 [onStateChange:] 代理方法
- (void)onStateChange:(ZegoAvatarServiceState)state {
}
通过 onStateChange 收到初始化 ZegoAvatarService
成功的通知后:
IZegoAvatarView
实例对象,用于后续展示虚拟形象创建传入虚拟人物形象的外观数据(捏脸、换装、妆容等)。// 1 创建一个 IZegoAvatarView 实例对象,用于后续展示虚拟形象创建传入虚拟人物形象的外观数据(捏脸、换装、妆容等)。
id<IZegoAvatarView> avatarView = [[ZegoAvatarService sharedInstance] createAvatarView:self.view.bounds];
[self.view addSubview:(UIView *)avatarView];
NSString *resourcePath = [NSBundle mainBundle].resourcePath;
// 传入模型路径,以 Q 版男生模型为例,Q 版女生模型请使用 Girl.prefab
NSString *characterPath = [NSString stringWithFormat:@"%@%@", resourcePath, @"/assets/qhuman.bundle/AssetBundles/bundle/qbody/Boy.prefab"];
// 2 传入模型绝对路径,创建一个虚拟形象。
id<IZegoCharacter> character = [[ZegoAvatarService sharedInstance] createCharacter:characterPath];
// 3 将 character 添加到 avatarView,设置虚拟形象视图,让虚拟形象上屏。
[avatarView setCharacter:character];
到此为止,您已成功构建一个简单的虚拟形象 App。接下来,您可通过以下文档, 进一步体验 Avatar 虚拟形象功能:
联系我们
文档反馈