How to convert UIImage to CCSprite?

Hi everyone.

I want to bring the image from the Internet in app.

So…

UIImage* img = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];

But, I don’t know convert UIImage to CCSPrite.
How to convert UIImage to CCSprite?

The sequence is:
# CCImage::initWithImageData(void* pData, int nDataLen, …)
# CCTexture2D::initWithImage(CCImage* uiImage);
# CCSprite::initWithTexture(CCTexture2D* pTexture);

sorry

Walzer Wang wrote:

The sequence is:
# CCImage::initWithImageData(void* pData, int nDataLen, …)
# CCTexture2D::initWithImage(CCImage* uiImage);
# CCSprite::initWithTexture(CCTexture2D* pTexture);

I am sorry.
I am not understand your apply.

How to get pData and nDataLength?

Please, you write in detail about subject.
You will appreciate in the attention.
Thank you.

I think you can get the image row data from UIImage.CGImage property.

1 Like

CCImage::initWithImageData is looking for a CCData object as the first param, but CCData is not fully implemented. You can only create a CCData by using dataWithContentsOfFile. As far as I can tell, you’d have to load in the UIImage data as NSData, save that as a file, then create a CCData from that saved file. I’m I getting this right?

//@todo implement
CCData* CCData::dataWithBytes(unsigned char *pBytes, int size)
{
CC_UNUSED_PARAM(pBytes);
CC_UNUSED_PARAM(size);
return NULL;
}

Figured out a work around. Once you load it, save the image to the local device. You can use CCSprite::spriteWithFileName to load it back in.

NSData fileData = ;
NSArray
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString documentsDirectory = ;
NSString
filePath = [NSString stringWithFormat:"%/%", documentsDirectory,“filename.png”];
BOOL didWrite = [fileData writeToFile:filePath atomically:YES];

Then later…
CCSprite* sprite = CCSprite::spriteWithFile(filePath);

Only way I could load in external images. CCData does not work with NSData.

How do you get images from URL and save them to the local disk on Android? Your method is for iOS only. It is not portable. I am looking for android solution.

If anyone interested, I’ve done it using the following ugly hack:

@

// Your code

struct Hack
{
UIImage* uiImage;
};

Hack hack;
hack.uiImage = imgToDisply;
CCImage* ccImage = new CCImage();
ccImage~~>initWithUiImage;
CCTexture2D* texture = new CCTexture2D;
texture~~>initWithImage(ccImage);
delete ccImage;

CCSprite* sprite = CCSprite::spriteWithTexture(texture);

// CCImage.h
// Add the following:

// For IOS
bool initWithUiImage(void* hack);

// CCImage_ios.mm

struct Hack
{
UIImage* uiImage;
};

bool CCImage::initWithUiImage(void* hackPtr)
{
Hack* hack = (Hack*)hackPtr;

CGImageRef CGImage = [hack->uiImage CGImage];

tImageInfo info = {0};

bool bRet = _initWithImage(CGImage, &info);

if (bRet)
{
m_nHeight = (short)info.height;
m_nWidth = (short)info.width;
m_nBitsPerComponent = info.bitsPerComponent;
m_bHasAlpha = info.hasAlpha;
m_bPreMulti = info.isPremultipliedAlpha;
m_pData = info.data;
}

return bRet;
}

@

Super ugly, but works :slight_smile:

I’m a bit late to the party, but here’s a solution I used.
It’s a mix of Obj-C and C++, so hopefully that’s not much of an issue for you.

/* Get the raw data from the UIImage /
CGImageRef imageRef = ;
NSUInteger width = CGImageGetWidth;
NSUInteger height = CGImageGetHeight;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB;
unsigned char
rawData = (unsigned char**) calloc);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel** width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

/* create a texture with the raw data, and use that to create a CCSprite*/
CCTexture2D tempTexture = new CCTexture2D;
tempTexture->initWithData);
CCSprite
finalSprite = CCSprite::spriteWithTexture(tempTexture);

Enjoy!

Anthony

This has been working good for me in most cases. I am taking the picture obtained from the camera and using that in my program. I am testing on some older devices iPh4 & 3GS and if I take a picture using the normal backside camera the image is black. If i use the front facing camera it works as expected. Also on newer devices iPh5, iPad 2, 3, Mini both work as expected. Any ideas why I would be receiving a black image once its back in cocos2dx on older devices?

Mike M wrote:

Anthony
>
This has been working good for me in most cases. I am taking the picture obtained from the camera and using that in my program. I am testing on some older devices iPh4 & 3GS and if I take a picture using the normal backside camera the image is black. If i use the front facing camera it works as expected. Also on newer devices iPh5, iPad 2, 3, Mini both work as expected. Any ideas why I would be receiving a black image once its back in cocos2dx on older devices?

I can’t say for certain since I haven’t captured data from the camera to display in cocos2d before.

My first guess would be that older devices may have different formats. Maybe the bytes per pixel is 3 on older devices instead of 4 on newer devices.

Mike M wrote:

Anthony
>
This has been working good for me in most cases. I am taking the picture obtained from the camera and using that in my program. I am testing on some older devices iPh4 & 3GS and if I take a picture using the normal backside camera the image is black. If i use the front facing camera it works as expected. Also on newer devices iPh5, iPad 2, 3, Mini both work as expected. Any ideas why I would be receiving a black image once its back in cocos2dx on older devices?

I can’t say for certain since I haven’t captured data from the camera to display in cocos2d before.

My first guess would be that older devices may have different formats. Maybe the bytes per pixel is 3 on older devices instead of 4 on newer devices.

Thanks for the reply, I actually just 2 minutes ago figured out what was going on and it was rather simple fix. It seems the image being taken was 1936x2592 way larger then would ever be needed on an iPhone for my purposes. So I simply scaled the CGImage and then used that and it all worked fine now.