(Solved)Png transparancy issue

Hi, I’m working on cocos2dx 3.12 version and working at android project.

I’m downloading png images from server using the following code -

void CategoryPage::downLoadCategoryImage(int d)
{
CCLog(“in downLoadImage 1111111111”);
previousValueCategory=d;
std::string strImage = previousValueCategory.asString()+“imgcat.png”;
cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();
CCLog(“in downLoadImage 333333333”);
{
CCLog(“vector imgUrl== %s”,imgCategoryUrlVector.at(previousValueCategory.asInt()).c_str());
CCLog(“in downLoadImage 4444444444”);
request->setUrl(imgCategoryUrlVector.at(previousValueCategory.asInt()).c_str());
CCLog(“in downLoadImage 55555555555”);
request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(CategoryPage::onCategoryImageDownLoaded));
request->setTag(strImage.c_str());
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();
}

}

After that I got the response -

void CategoryPage::onCategoryImageDownLoaded(cocos2d::network::HttpClient* pSender, cocos2d::network::HttpResponse* pResponse)
{
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
cocos2d::network::HttpResponse* response = pResponse;

    if (!response)
    {
        CCLog("No Response");
        return;
    }
    int statusCode = response->getResponseCode();

    char statusString[64] = {};
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
    CCLog("response code: %d", statusCode);

    if (!response->isSucceed())
   
    {
        CCLog("response failed");
     
        MessageBox("error in downloaded", "Image");
      //  download->setVisible(false);
        return;
    }
    std::vector<char>*buffer = response->getResponseData();

    Image * img=new Image();
    img->initWithImageData((const unsigned char *)(&buffer->front()), buffer->size());

  //  MessageBox("Image downloaded", "Image");
    // Save image file to device.
    std::string writablePath = CCFileUtils::sharedFileUtils()->getWritablePath();
    writablePath.append(response->getHttpRequest()->getTag());
    img->saveToFile(writablePath.c_str());

    //Now create Sprite from downloaded image shoppinglistitem

    imageView = ui::ImageView::create(writablePath.c_str());
  //listView->setBackGroundImage("offers.png");
    imageView->setPosition(imageView->getContentSize()/2);
    imageView->setTouchEnabled(true);

     listView->pushBackCustomItem(imageView);//nil
      ////////////////////////////////////////////////////////////

/////////

}

In above code images are downloaded properly but png images are losing there transparency. Means all png images have black background.
Means transparent part of image becomes black.

please help me resolving this issue.

thanks.

Issue Solved by changing following line
img->saveToFile(writablePath.c_str());

to
img->saveToFile(writablePath.c_str(),false);

thanks.

1 Like