2013-02-06 100 views
0

我正在从服务器下载图像并将其显示在游戏场景中。我能够从服务器获取图像的CCTexture2D并将其显示在游戏场景中。问题是来自服务器的图像大小可能有所不同。但是我必须将该图像显示在预定义的帧CCSprite上。将CCTexture的框架设置为适合预定义的框架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。但是图像会被剪裁到不想要的所需帧。有人可以告诉我如何从服务器获取原始图像到所需的帧,而不会被裁剪。谢谢。

回答

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

提防鸿沟