在 Native 下远程加载资源使用,native.fileUtils 进行本地化存储,但是获取的 ImageAsset.data 的类型还是 HTMLImageElement,而不是 Native 下的:Uint8Array/ArrayBuffer
对于native项目,为了统一web和native的资源加载流程,引擎封装了一套HTMLImageElement,在jsb-adapter文件夹里面,你看到的应该是这个。
再借楼推广一波,cc_debuger_realtime,支持实时预览当前使用中的远程图片资源:【cc_debuger_realtime】重磅工具插件,cocoser 必备,免费试用
同时也支持实时预览native下运行时的writablePath,即通过native.fileUtils下载到本地的资源全都可以查看,甚至可以下载到电脑进行分析。
存储时需要图片信息的 Uint8Array, native.fileUtils.writeDataToFile(buffer, savePath),Native 下还是:HTMLImageElement,有什么办法获取图片信息(Uint8Array)?
对于下载图片,你可以直接使用native.Downloader啊
let _downloadingList = {};//正在下载的
/**
* 下载文件到本地
*/
export function downloadFile(url:string, savePath:string, callback:(result:boolean,data:any)=>void){
if(!NATIVE){
return;
}
if(!_funcs.isValidURL(url)){
console.log("_fileCacher,downloadFile url不合法",url)
callback(false,null);
return
}
if(_downloadingList[url]){
_downloadingList[url].push(callback)
return;
}
_downloadingList[url] = [callback];
var downloader = new native.Downloader();
downloader.onSuccess = (task:native.DownloadTask)=>{
for(let cb of _downloadingList[url]){
cb(true,savePath)
}
delete _downloadingList[url];
}
log("_fileCacher,downloadFile url",url)
downloader.onError = function(task: native.DownloadTask, errorCode: number, errorCodeInternal: number, errorStr: string){
log("_fileCacher,download fail",errorCode,errorCodeInternal,errorStr)
for(let cb of _downloadingList[url]){
cb(false,url)
}
delete _downloadingList[url];
}
downloader.createDownloadTask(url, savePath);//创建下载任务
}
好的,感谢感谢