UIImage 到 cocos2dx 中的 Sprite?
UIImage to Sprite in cocos2dx?
首先,我将 URL 中的图像保存在文档目录中:
NSURL *imageURL = [NSURL URLWithString:userImage];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"image name.png"];
[UIImagePNGRepresentation(image) writeToFile:savedImagePath atomically:YES];
然后我想在 cpp 中将此图像检索为 Sprite:
std::string writablePath2 = CCFileUtils::getInstance()->getWritablePath();
writablePath2.append("your saved image.png");
Rect rect2 = Rect(visibleSize.width/2 - 200, visibleSize.height/2 -200 , 500, 500 );
Sprite *screen_shot = Sprite::create(writablePath2,rect2);
screen_shot->setPosition(visibleSize.width/2 ,origin.y + visibleSize.height/2 - 100);
this->addChild(screen_shot,2);
但是显示的是黑精灵。
1) 从文件路径创建 Texture2D
2) 从 Texture2D 创建 Sprite
这是代码
std::string imagPath = CCFileUtils::getInstance()->getWritablePath();
imagPath.append("your image name.png");
Texture2D* objTexture2D = new Texture2D();
Image* image = new Image();
JpgImage->initWithImageFile(imagPath);
objTexture2D->initWithData(image->getData(),image->getDataLen(),Texture2D::PixelFormat::RGB888,image->getWidth(),image->getHeight(),Size(image->getWidth(),image->getHeight()));
Sprite *spr = Sprite::createWithTexture(objTexture2D);
spr->setPosition(origin.x + visibleSize.width/2 , visibleSize.height/2);
this->addChild(spr,2);
给你....
首先,我将 URL 中的图像保存在文档目录中:
NSURL *imageURL = [NSURL URLWithString:userImage];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"image name.png"];
[UIImagePNGRepresentation(image) writeToFile:savedImagePath atomically:YES];
然后我想在 cpp 中将此图像检索为 Sprite:
std::string writablePath2 = CCFileUtils::getInstance()->getWritablePath();
writablePath2.append("your saved image.png");
Rect rect2 = Rect(visibleSize.width/2 - 200, visibleSize.height/2 -200 , 500, 500 );
Sprite *screen_shot = Sprite::create(writablePath2,rect2);
screen_shot->setPosition(visibleSize.width/2 ,origin.y + visibleSize.height/2 - 100);
this->addChild(screen_shot,2);
但是显示的是黑精灵。
1) 从文件路径创建 Texture2D
2) 从 Texture2D 创建 Sprite
这是代码
std::string imagPath = CCFileUtils::getInstance()->getWritablePath();
imagPath.append("your image name.png");
Texture2D* objTexture2D = new Texture2D();
Image* image = new Image();
JpgImage->initWithImageFile(imagPath);
objTexture2D->initWithData(image->getData(),image->getDataLen(),Texture2D::PixelFormat::RGB888,image->getWidth(),image->getHeight(),Size(image->getWidth(),image->getHeight()));
Sprite *spr = Sprite::createWithTexture(objTexture2D);
spr->setPosition(origin.x + visibleSize.width/2 , visibleSize.height/2);
this->addChild(spr,2);
给你....