在项目开发过程中有时候需要将远程连接的图片加载到本地存放,供以后使用,但是在cocos 官方文档里面没有详细的介绍存放到本地的方法,现在在这里讲述如何将远程资源加载到本地进行保存
首先新建一个对象类专门用来管理加载图片的类
ImageLoader.js
var md = require("./md5");
cc.Class({
extends: cc.Component,
statics : {
loadImage : function (url,callback){
cc.loader.load(url,function (err,tex) {
var spriteFrame = new cc.SpriteFrame(tex, cc.Rect(0, 0, tex.width, tex.height));
callback(spriteFrame);
});
},
imageLoadTool(url, callback){
var dirpath = jsb.fileUtils.getWritablePath() + 'customHeadImage/';
console.log("dirpath ->",dirpath);
let md5URL = md(url);
var filepath = dirpath + md5URL + '.jpg';
console.log("filepath ->",filepath);
function loadEnd(){
cc.loader.load(filepath, function(err, tex){
if( err ){
cc.error(err);
}else{
var spriteFrame = new cc.SpriteFrame(tex);
if( spriteFrame ){
spriteFrame.retain();
callback(spriteFrame);
}
}
});
}
if( jsb.fileUtils.isFileExist(filepath) ){
cc.log('Remote is find' + filepath);
loadEnd();
return;
}
var saveFile = function(data){
if( typeof data !== 'undefined' ){
if( !jsb.fileUtils.isDirectoryExist(dirpath) ){
jsb.fileUtils.createDirectory(dirpath);
}else{
console.log("路径exist");
}
// new Uint8Array(data) writeDataToFile writeStringToFile
if( jsb.fileUtils.writeDataToFile( new Uint8Array(data) , filepath) ){
cc.log('Remote write file succeed.');
loadEnd();
}else{
cc.log('Remote write file failed.');
}
}else{
cc.log('Remote download file failed.');
}
};
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
cc.log("xhr.readyState " +xhr.readyState);
cc.log("xhr.status " +xhr.status);
if (xhr.readyState === 4 ) {
if(xhr.status === 200){
//responseType一定要在外面设置
// xhr.responseType = 'arraybuffer';
saveFile(xhr.response);
}else{
saveFile(null);
}
}
}.bind(this);
//responseType一定要在外面设置
xhr.responseType = 'arraybuffer';
xhr.open("GET", url, true);
xhr.send();
},
},
});
这里的第一句的var md = require("md5"); 引用了md5.js
这里提供md5.js的源码直接下载即可:
!function(n){"use strict";function t(n,t){var r=(65535&n)+(65535&t);return(n>>16)+(t>>16)+(r>>16)<<16|65535&r}function r(n,t){return n<>>32-t}function e(n,e,o,u,c,f){return t(r(t(t(e,n),t(u,f)),c),o)}function o(n,t,r,o,u,c,f){return e(t&r|~t&o,n,t,u,c,f)}function u(n,t,r,o,u,c,f){return e(t&o|r&~o,n,t,u,c,f)}function c(n,t,r,o,u,c,f){return e(t^r^o,n,t,u,c,f)}function f(n,t,r,o,u,c,f){return e(r^(t|~o),n,t,u,c,f)}function i(n,r){n[r>>5]|=128<>>9<<4)]=r;var e,i,a,d,h,l=1732584193,g=-271733879,v=-1732584194,m=271733878;for(e=0;e>5]>>>t%32&255);return r}function d(n){var t,r=[];for(r[(n.length>>2)-1]=void 0,t=0;t>5]|=(255&n.charCodeAt(t/8))<16&&(o=i(o,8*n.length)),r=0;r<16;r+=1)u[r]=909522486^o[r],c[r]=1549556828^o[r];return e=i(u.concat(d(t)),512+8*t.length),a(i(c.concat(e),640))}function g(n){var t,r,e="";for(r=0;r>>4&15)+"0123456789abcdef".charAt(15&t);return e}function v(n){return unescape(encodeURIComponent(n))}function m(n){return h(v(n))}function p(n){return g(m(n))}function s(n,t){return l(v(n),v(t))}function C(n,t){return g(s(n,t))}function A(n,t,r){return t?r?s(t,n):C(t,n):r?m(n):p(n)}"function"==typeof define&&define.amd?define(function(){return A}):"object"==typeof module&&module.exports?module.exports=A:n.md5=A}(this);
//# sourceMappingURL=md5.min.js.map