Forum > C++ > How to convert UIImage to CCSprite?

How to convert UIImage to CCSprite?

By satmeet Posted 2011-09-18 21:59 Comments 13
satmeet
  • Posts: 9

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?

walzer
  • Posts: 1735
  • Location: Xiamen, China

#1 RE: 2011-09-19 00:05

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

Less is More. Keep Focus.

satmeet
  • Posts: 9

#2 RE: 2011-09-19 00:39

sorry

satmeet
  • Posts: 9

#3 RE: 2011-09-19 01:02

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.

walzer
  • Posts: 1735
  • Location: Xiamen, China

#4 RE: 2011-09-19 01:06

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

Less is More. Keep Focus.

jmarziani
  • Posts: 7

#5 RE: 2012-04-25 19:13

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;
}

jmarziani
  • Posts: 7

#6 RE: 2012-04-25 19:36

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.

emmyc
  • Posts: 73
  • Location: US

#7 RE: 2012-05-02 09:50

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.

enjoy work!

Obg1
  • Posts: 110

#8 RE: 2012-06-18 06:31

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 :)

AnthonyBongers
  • Posts: 4

#9 RE: 2012-07-11 14:18

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!

gerardm
  • Posts: 21

#10 RE: 2013-02-26 14:50

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?

AnthonyBongers
  • Posts: 4

#11 RE: 2013-02-26 16:37

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.

AnthonyBongers
  • Posts: 4

#12 RE: 2013-02-26 16:37

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.

gerardm
  • Posts: 21

#13 RE: 2013-02-26 16:40

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.

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps