0

サーバーから画像をダウンロードしてゲームシーンに表示しています。サーバーから画像の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 にサイズ変更しています。しかし、画像は望ましくない目的のフレームにトリミングされます。トリミングせずにサーバーから目的のフレームに元の画像を取得する方法を教えてください。ありがとう。

4

1 に答える 1

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による除算に注意してください:)

于 2013-02-06T14:25:42.847 に答える