设置CCTexture的框架以适合预定义的框架CCSprite

时间:2013-02-06 12:44:08

标签: iphone ios6 cocos2d-iphone

我从服务器下载图像并在游戏场景中显示。我能够从服务器获取图像的CCTexture2D并在游戏场景中显示它。问题是来自服务器的图像大小可能不同。但我必须将该图像显示在预定义的帧CCSprite上。

CCSprite *temp = [CCSprite spriteWithTexture:[[CCTexture2D alloc] initWithImage:[UIImage imageWithData:data] resolutionType:kCCResolutioniPhoneFourInchDisplay]];
CCRenderTexture *test=[CCRenderTexture renderTextureWithWidth:70 height:70];   //set proper width and height
[test begin];
[temp draw];
[test end];
UIImage *img=[test getUIImageFromBuffer];
sprite_Temp =[CCSprite spriteWithCGImage:img.CGImage key:@"1"];
sprite_Temp.tag = K_TagUserImage;
sprite_Temp.scale=1;
sprite_Temp.position=ccp(432,273);
[self addChild:sprite_Temp z:1];

我正在使用此代码将CCTexture2D的大小调整为预定义的帧CCSprite。但是图像被裁剪到所需的帧,这是不想要的。有人可以告诉我如何将原始图像从服务器获取到所需的帧而不会被裁剪。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试:

 CCSprite *temp = [CCSprite spriteWithTexture:[[CCTexture2D alloc] initWithImage:[UIImage imageWithData:data] resolutionType:kCCResolutioniPhoneFourInchDisplay]];
float scaleX = 70./temp.contentSize.width;
float scaleY = 70./temp.contentSize.height;
// if you want to preserve the original texture's aspect ratio
float scale = MIN(scaleX,scaleY);
temp.scale = scale;
// or if you want to 'stretch-n-squeeze' to 70x70
temp.scaleX = scaleX;
temp.scaleY = scaleY;
// then add the sprite *temp

通常的免责声明:未经测试,从内存中完成,谨防0除以:)