cocos creator截图问题

问题

笔者使用cocos creator开发了一款小游戏,其中需要对某个特定node截图,使用的方式是官方demo里的截图方式,这个截图在微信小游戏上没什么问题,但是在native(iOS)工程上会出现截图不全的问题

解决

经过一番探究发现是坐标系的转换问题,由于iOS的坐标原点在左上角,所以截图代码需要做一些改动。
demo里的代码

1
2
3
4
5
6
7
copyRenderTex() {
var width = this.targetNode.getComponent(UITransform).width;
var height = this.targetNode.getComponent(UITransform).height;
var worldPos = this.targetNode.getWorldPosition();
this._buffer = this.rt.readPixels(Math.round(worldPos.x), Math.round(worldPos.y), width, height);
this.showImage(width, height);
}

将其中的Y坐标调整下即可:
this.rt.height - Math.round(worldPos.y) - height