2013-02-06 42 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 :)

提防鴻溝