UIImage与CCSprite互相转换

http://ityran.com/thread-27-1-1.html  来自于

有时候为了方便需要是不是需要把UIImage转换成精灵 ,而有时候又需要把精灵转换成UIImage, 有木有为这些在烦恼的,有的话顶起来

-(CCSprite *) convertImageToSprite:(UIImage *) image {

        CCTexture2D *texture = [[CCTexture2D alloc] initWithImage:image];

        CCSprite    *sprite = [CCSprite spriteWithTexture:texture];

        [texture release];

}

-(UIImage *) convertSpriteToImage:(CCSprite *)sprite {

   CGPoint p = sprite.anchorPoint;   

   [sprite setAnchorPoint:ccp(0,0)];  

   CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];

   [renderer begin];

   [sprite visit];  

   [renderer end];   

   [sprite setAnchorPoint:p];  

   return [UIImage imageWithData:[renderer getUIImageAsDataFromBuffer:kCCImageFormatPNG]];

}

posted @ 2011-09-21 10:15  痴人指路  阅读(1421)  评论(0编辑  收藏  举报