cocos2dx 有没有方法直接搜索手机里面的图片 然后加载出来?
可以遍历手机里面的图片,提供一个参考方法。。。
void Sprite::findDirFile(std::string path,const std::string& spriteFrameName)
{
DIR *pDir ;
struct dirent *ent ;
char childpath;
if((pDir=opendir(path.c_str())) ==NULL) return;
memset(childpath,0,sizeof(childpath));
while((ent=readdir(pDir))!=NULL)
{
if(ent->d_type & DT_DIR)
{
if(strncmp(ent->d_name,".",1)==0)
continue;
sprintf(childpath,"%s/%s",path.c_str(),ent->d_name);
log("findDirFile childpath:%s",childpath);
findDirFile(childpath,spriteFrameName);
}
else
{
std::string fileName=ent->d_name;
if (fileName.find(".plist") !=std::string::npos) {
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
if(dict.find("frames")!=dict.end()){
ValueMap& framesDict = dict"frames"].asValueMap();
for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)
{
std::string FrameName = iter->first;
log("fileName:%s FrameName:%s spriteFrameName:%s",fileName.c_str(),FrameName.c_str(),spriteFrameName.c_str());
if (FrameName.compare(spriteFrameName)==0) {
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(fileName);
closedir(pDir);
return;
}
}
}
}
}
}
closedir(pDir);
}
没必要这么做吧
不然有啥更好方法?
这个方法对安卓或者ios手机通用?路径不是得自己写?
我是想知道为什么要这么做
我也想知道哦
就是传入一个 路径,然后会递归的搜索这个目录及其子目录里面的文件,你只要看懂这个函数,然后改成自己需要的,比较简单