1

有谁知道如何将 UIImage 转换为 cocos2d-x CCSprite。我最近的尝试是: 1. 在手机上将 UIImage 存储为 png 2. 将 png 加载为 CCSprite

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

但这在 CCObject 保留功能中崩溃了

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

    ++m_uReference;
}

而且我不明白 Walzer Wang 的建议是如何工作的 http://cocos2d-x.org/boards/6/topics/3922

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

3 回答 3

2
CCSprite* getCCSpriteFromUIImage(UIImage *photo) {
    CCImage *imf =new CCImage();
    NSData *imgData = UIImagePNGRepresentation(photo);
    NSUInteger len = [imgData length];
    Byte *byteData = (Byte*)malloc(len);
    memcpy(byteData, [imgData bytes], len);
    imf->initWithImageData(byteData,imgData.length);
    imf->autorelease();
    CCTexture2D* pTexture = new CCTexture2D();
    pTexture->initWithImage(imf);
    pTexture->autorelease();
    CCSprite *sprit = new CCSprite();
    sprit->createWithTexture(pTexture);
    sprit->autorelease();
    DebugLog("size :%f :%f ",sprit->getContentSize().width , sprit->getContentSize().height);

    return sprit;
}
于 2013-02-28T11:11:37.430 回答
0

我为自己的问题找到了解决方案。在 CCDirector 初始化之前不能创建 CCSprite。缺少一些配置设置,cocos 实例化后立即释放镜像。

于 2012-06-29T07:36:12.683 回答
0

先保存uiimage到documents目录

现在使用 getWritablePath 获取它

Texture2D* newTexture2D = new Texture2D();
Image* JpgImage     = new Image();

JpgImage->initWithImageFile("your image path.jpg");
newTexture2D->initWithData(JpgImage->getData(),JpgImage->getDataLen(),Texture2D::PixelFormat::RGB888,JpgImage->getWidth(),JpgImage->getHeight(),Size(JpgImage->getWidth(),JpgImage->getHeight()));
于 2015-02-27T05:51:00.273 回答