getPicByUrl(callback: Function, url: string) {
测试用的地址来的
// if (!url) url = "http://xx.xx/html5/share1.png";
let fileDir = `${jsb.fileUtils.getWritablePath()}madherogo`;
var currentDate = new Date();
var fileName = currentDate.getFullYear() + "" + (currentDate.getMonth() + 1) + ""
+ currentDate.getDate() + "" + currentDate.getHours() + "" + currentDate.getMinutes() + currentDate.getSeconds();
fileName = fileName + ".png";
let fullPath = `${fileDir}/${fileName}`;
if (!jsb.fileUtils.isDirectoryExist(fileDir)) {
jsb.fileUtils.createDirectory(fileDir);
}
if (jsb.fileUtils.isFileExist(fullPath)) {
jsb.fileUtils.removeFile(fullPath);
}
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
if (xhr.response) {
let u8a = new Uint8Array(xhr.response);
// let success = jsb.fileUtils.writeValueMapToFile(u8a, fullPath);
let success = jsb.fileUtils.writeDataToFile(u8a, fullPath);
// let picData = this.filpYImage(u8a, 500, 400);
// let success = jsb.saveImageData(picData, 500, 400, fullPath);
if (success) {
// eslint-disable-next-line no-console
console.log('截屏成功,fullPath,width,height = ', fullPath);
callback(fullPath);
}
else {
cc.error('截屏失败!');
}
}
}
};
xhr.responseType = 'arraybuffer';
xhr.open("GET", url, true);
xhr.send();
}