微信小游戏需要实现一个功能,使用摄像头抓取画面,作为游戏背景。使用了下面的代码,在android上没有问题,在ios上确显示不了图片
openCamera() {
var self = this;
let camera = wx.createCamera({
x: -2000,
y: -2000,
width: 1,
height: 1,
devicePosition: 'back',
flash: 'auto',
size: 'small',
success: function () {
console.log("摄像头打开成功");
camera.listenFrameChange();//开始监听帧数据
var listener = camera.onCameraFrame((frame) => {
var data = new Uint8ClampedArray(frame.data);
let size = cc.size(frame.width, frame.height);
let cameraTexture = new cc.RenderTexture();
cameraTexture.initWithData(data, cc.Texture2D.PixelFormat.RGBA8888, size.width, size.height);
self.bgNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(cameraTexture, new cc.Rect(0, 0, size.width, size.height), false, cc.Vec2.ZERO, size);
});
},
fail: function (res) {
console.log(res);
console.log("摄像头失败");
}
});
},
就这代码在安卓系统上能够完美实现,ios上确不行,不知道哪个api不兼容 cocos版本2.4.4
