How can i get an image from iphone device camera to cocos2dx CCSprite


#1

I’m a newbie, can some give me a demo?


#2

You will have to use native code to talk to the camera, get the file into a PNG or JPG and store in the Applicaiton’s Documents LIbrary. After that you should be able to use CCFileUtils to locate it in Documents and read it into the Texture Cache.


#3

Thanks Cory Trese。
I have find a way to do this.

UIImage* image = [info valueForKey:UIImagePickerControllerOriginalImage];

CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CCLog(“i,i”,width,height);
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);
cocos2d::CCTexture2D *pickedImage = new cocos2d::CCTexture2D();
pickedImage->initWithData(rawData, cocos2d::kCCTexture2DPixelFormat_RGBA8888, width, height, cocos2d::CCSizeMake(width, height));

just fix the UIImage’s Orientation before do this.

Cory Trese wrote:

You will have to use native code to talk to the camera, get the file into a PNG or JPG and store in the Applicaiton’s Documents LIbrary. After that you should be able to use CCFileUtils to locate it in Documents and read it into the Texture Cache.