# 分享系统

xr-frame渲染的结果分享给好友是个很常见的需求,所以我们内置了分享系统ShareSystem来协助开发者快速截图和录制分享。

要在iOS设备上使用分享系统,需要客户端8.0.29以上版本。 录屏能力需要在基础库版本v3.1.1以上支持。

# 截取画布

分享系统提供了三种方式来截图:

// 判断当前客户端是否支持分享系统
const supported = scene.share.supported;

// 截取配置,可选`type`为`jpg`或`png`,在为`jpg`时,可配置`0~1`的`quality`
// 以下是默认值
const options: XrFrame.IShareCaptureOptions = {
  type: 'jpg',
  quality: 0.8
};

// 在鸿蒙上有BUG,已弃用,请使用下方异步接口!
const base64 = scene.share.captureToDataURL(options);
// 基础库`3.0.2`以上新增的异步接口,获取原始的`base64`字符串
const base64 = await scene.share.captureToDataURLAsync(options);

// 在鸿蒙上有BUG,已弃用,请使用异步接口!
const buffer = scene.share.captureToArrayBuffer(options);
// 基础库`3.0.2`以上新增的异步接口,直接获取转换后的`ArrayBuffer`
const buffer = await scene.share.captureToArrayBufferAsync(options);

// 获取本地路径的分享图片
// 第一个方法来处理本地路径,结束后释放文件
// 此回调在`2.27.3`基础库以及之前时是异步,之后兼容同步和异步
// 在基础库`3.0.2`之后,为异步接口
await scene.share.captureToLocalPath(options, fp => {});

// 直接唤起分享图片给好友
// 在基础库`3.0.2`之后,为异步接口
await scene.share.captureToFriends(options);

唤起分享给好友的结果如下:

# 录制画布

在基础库版本v3.1.1之后,分享系统提供了如下方式来录制:

// 录制配置,以下是默认值
// 这里建议根据实际情况,等比缩放`width`和`height`,防止丢帧
const options: XrFrame.IShareRecordOptions = {
  fps: 30,
  width: scene.width,
  height: scene.height,
  videoBitsPerSecond: 1000
};

// 启动录制
await scene.share.recordStart(options);

// 暂停录制
await scene.share.recordPause(options);

// 唤醒录制
await scene.share.recordResume(options);

// 停止录制,并保存到临时文件
const tempFilePath = await scene.share.recordFinishToTempFile(options);

// 停止录制,直接保存到相册
await scene.share.recordFinishToAlbum(options);

// 可以获取录制状态,来做用户提示
if (scene.share.recordState === xrFrameSystem.EShareRecordState.Recording) {
  ......
}