cocos creator2.1 截屏
shareResult ( ) {
let size = cc. winSize;
let width = Math. floor ( size. width) ;
let height = Math. floor ( size. height) ;
if ( CC_JSB ) {
let fileName = "share.jpg" ;
let filePath = jsb. fileUtils. getWritablePath ( ) + fileName;
if ( jsb. fileUtils. isFileExist ( filePath) ) {
jsb. fileUtils. removeFile ( filePath) ;
}
let cameraNode = new cc. Node ( ) ;
cameraNode. parent = cc. director. getScene ( ) ;
let camera = cameraNode. addComponent ( cc. Camera) ;
cameraNode. position = cc. v2 ( width / 2 , height / 2 )
camera. cullingMask = 0xffffffff ;
let texture = new cc. RenderTexture ( ) ;
texture. initWithSize ( width, height, cc. gfx. RB_FMT_S8 ) ;
camera. targetTexture = texture;
camera. render ( ) ;
let data = texture. readPixels ( ) ;
let picData = new Uint8Array ( width * height * 4 ) ;
let rowBytes = width * 4 ;
for ( let row = 0 ; row < height; row++ ) {
let srow = height - 1 - row;
let start = Math. floor ( srow * width * 4 ) ;
let reStart = row * width * 4 ;
for ( let i = 0 ; i < rowBytes; i++ ) {
picData[ reStart + i] = data[ start + i] ;
}
}
let success = jsb. saveImageData ( picData, width, height, filePath) ;
console. log ( "截图成功,图片保存在 ====>" , filePath) ;
if ( success) {
console. log ( "保存图像数据成功,文件: " + filePath) ;
}
else {
console. error ( "保存图像数据失败!" ) ;
}
}
} ,