CCImage created with initWithImageData becomes white after app goes background


#1

Hi,
Although I’ve been working for some time with cocos2d-x, this is my first post (and first major problem).
I’m using cocos2d-1.0.1-x-0.12.0.
Updating cocos2d-x is not an option now.

I’m loading an image from a buffer (filled with data downloaded from a URL) this way:
img->initWithImageData((void*)buffer.memory, (long)buffer.size, CCImage::kFmtPng);
Then creating a texture:
texture = new cocos2d::CCTexture2D(); texture->initWithImage(img);

This works fine on iOS.
However on Android whenever the app goes background and foreground again, the image becomes white (solid white, or transparent white).
This only occurs for that one sprite created with a CCImage from raw data, and not from regular sprites created from resources (with spriteWithSpriteFrameName).

Thanks.


#2

I found a way around: storing the CCImage (as many as I need) and calling again CCTexture2D::initWithImage() whenever the app returns to the foreground.


#3

Can you please give me a little more information about your solution?


#4

Sorry, I didn’t see your post earlier.

// create image(s) from data (in my case downloaded), this way:
CCImage* image = new CCImage();
image->initWithImageData((void*)buffer.memory, (long)buffer.size, CCImage::kFmtPng);    // can also use initWithImageFile()

// then store image(s) in an array or vector
...
// create texture and sprite from image
cocos2d::CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addUIImage(image, key);
CCSprite* sprite = CCSprite::spriteWithTexture(texture);
addChild(sprite);

Then whenever app goes back into the foreground, call:

texture->initWithImage(image);