Search code examples
cocos2d-iphoneuiimageccspritecocos2d-x

cocos2d-x convert UIImage to CCSprite


has anyone an idea how to convert a UIImage to cocos2d-x CCSprite. My latest attempt was was: 1. Store the UIImage as png on the phone 2. Load the png as a CCSprite

[UIImagePNGRepresentation(photo) writeToFile:imagePath atomically:true];
CCSprite *sprite = CCSprite::spriteWithFile(imagePath);

But this crashed in CCObject retain function

void CCObject::retain(void)
{
    CCAssert(m_uReference > 0, "reference count should greater than 0");

    ++m_uReference;
}

And I do not understand how Walzer Wangs suggestion works http://cocos2d-x.org/boards/6/topics/3922

CCImage::initWithImageData(void* pData, int nDataLen, ...)
CCTexture2D::initWithImage(CCImage* uiImage);
CCSprite::initWithTexture(CCTexture2D* pTexture);

Solution

  • I got the solution for my own Problem. You can't create a CCSprite before the CCDirector is initialized. There are missing some config settings and cocos releases the image right after it is instantiated.