我正在尝试从Facebook服务器加载CCSprite。但是当它加载时,它会显示为黑色图像。我不知道为什么。我认为CURL缓冲它是0我离开我的代码。我不知道这是否是一种简单的方法。
编辑:我已经尝试了主线程,也是黑色
注意:我在pthread上运行它。
//called at the end of init
pthread_t tid1;
pthread_create(&tid1, NULL, &loadSync, this);
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
exit(EXIT_FAILURE);
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
//This class it's called helpBlock
static void *loadSync(void *args) {
helpBlock *thiz = (helpBlock*)args;
CURL *curl_handle;
struct MemoryStruct chunk;
/* will be grown as needed by the realloc above /
chunk.size = 0; / no data at this point */
chunk.memory = (char*)malloc(1);
chunk.size = 0;
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://graph.facebook.com/4/picture?width=60&height=60");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
curl_easy_perform(curl_handle);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
//mySprite->setTexture(CCTextureCache::sharedTextureCache()->addImage("newImage.png"));
CCLog("%s - %d", chunk.memory, chunk.size);
CCImage* img = new CCImage;
img->initWithImageData((void*)chunk.memory, (long)chunk.size, CCImage::kFmtPng);
cocos2d::CCTexture2D* texture = new cocos2d::CCTexture2D();
texture->initWithImage(img);
thiz->firstFriend->frontSprite->setTexture(texture);
if(chunk.memory)
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return NULL;
}