zoukankan      html  css  js  c++  java
  • 图片的本地存储和读取问题

    本地存储完全没问题

    loadNative = function(url, callback){
        var dirpath =  jsb.fileUtils.getWritablePath() + 'img/';
        var filepath = dirpath + MD5(url) + '.png';
    
        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);
                }
    
                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){
                    xhr.responseType = 'arraybuffer';
                    saveFile(xhr.response);
                }else{
                    saveFile(null);
                }
            }
        }.bind(this);
        xhr.open("GET", url, true);
        xhr.send();
    };

  • 相关阅读:
    柱状图最大的矩形
    单词搜索
    最小覆盖子串
    颜色分类
    编辑距离
    X的平方根
    二进制求和
    最大子序和
    N皇后
    java8-14-时间API
  • 原文地址:https://www.cnblogs.com/luorende/p/9052393.html
Copyright © 2011-2022 走看看