Jung Jun Lee How to convert UIImage to CCSprite?
Posts 9
Added by Jung Jun Lee over 1 year ago

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?

Zhe Wang RE: How to convert UIImage to CCSprite?
Posts 1642
Location Amoy, China
Added by Zhe Wang over 1 year ago
The sequence is:
  1. CCImage::initWithImageData(void* pData, int nDataLen, ...)
  2. CCTexture2D::initWithImage(CCImage* uiImage);
  3. CCSprite::initWithTexture(CCTexture2D* pTexture);

Enjoy Coding, Enjoy Life.

Jung Jun Lee RE: How to convert UIImage to CCSprite?
Posts 9
Added by Jung Jun Lee over 1 year ago

sorry

Jung Jun Lee RE: How to convert UIImage to CCSprite?
Posts 9
Added by Jung Jun Lee over 1 year ago

Walzer Wang wrote:

The sequence is:
  1. CCImage::initWithImageData(void* pData, int nDataLen, ...)
  2. CCTexture2D::initWithImage(CCImage* uiImage);
  3. 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.

Zhe Wang RE: How to convert UIImage to CCSprite?
Posts 1642
Location Amoy, China
Added by Zhe Wang over 1 year ago

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

Enjoy Coding, Enjoy Life.

Jason M RE: How to convert UIImage to CCSprite?
Posts 7
Added by Jason M about 1 year ago

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

Jason M RE: How to convert UIImage to CCSprite?
Posts 7
Added by Jason M about 1 year ago

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 = [YOUR IMAGE DATA];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
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.

Emmy Chen RE: How to convert UIImage to CCSprite?
Posts 42
Added by Emmy Chen about 1 year ago

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.

Oren Bengigi RE: How to convert UIImage to CCSprite?
Posts 107
Added by Oren Bengigi 11 months ago

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(&hack);
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 :)

Anthony Bongers RE: How to convert UIImage to CCSprite?
Posts 4
Added by Anthony Bongers 11 months ago

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 = [tempImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char
) calloc(height * width * 4, sizeof(unsigned char));
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(rawData, kCCTexture2DPixelFormat_RGBA8888, width, height, CCSizeMake(width, height));
CCSprite *finalSprite = CCSprite::spriteWithTexture(tempTexture);

Enjoy!

Mike M RE: How to convert UIImage to CCSprite?
Posts 18
Added by Mike M 3 months ago

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?

Anthony Bongers RE: How to convert UIImage to CCSprite?
Posts 4
Added by Anthony Bongers 3 months ago

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.

Anthony Bongers RE: How to convert UIImage to CCSprite?
Posts 4
Added by Anthony Bongers 3 months ago

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 RE: How to convert UIImage to CCSprite?
Posts 18
Added by Mike M 3 months ago

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.


(1-13/13)