This would be really helpful if anyone knows how to get a UIImage back from an existing Texture2D.
How to make UIImage from Texture2D
(8 posts) (4 voices)-
Posted 2 years ago #
-
Jason Booth's RenderTexture class could be used for this. It saves the texture to a file but part of doing that is creating a UIImage so it wouldn't be hard to hack your own texture to UIImage method.
Posted 2 years ago # -
Thanks for the quick reply :)
I've been messing with the RenderTexture class for a few hours before I posted my request above.
I cannot get it to work without actually writing/reading the file.
I made a copy of the "saveBuffer" method and named it "-(UIImage *)bufferAsUIImage"
and just removed the stuff about writing to a file and then return the UIImage...
But at runtime it fails with the error "CGSConvertRGBX8888toRGBA8888"Here is the relevant stack trace from the point I try to make a new Texture2D from the UIImage I got from capturing a sprite:
#0 0x00299d00 in CGSConvertRGBX8888toRGBA8888
#1 0x002601f4 in RGBA32_image
#2 0x04fa4ed8 in ripl_Mark
#3 0x04fa9306 in ripl_BltImage
#4 0x04f928ac in ripc_RenderImage
#5 0x04fa1bd8 in ripc_DrawImage
#6 0x00207ce4 in CGContextDrawImage
#7 0x00076bd8 in -[Texture2D(Image) initWithImage:] at Texture2D.m:260Also, I COULD just use the saveBuffer method and read it back into a UIImage but for some reason I lose alpha channel info even when using PNG format.
Posted 2 years ago # -
OK... I got it all figured out :)
Here are the changes I made to RenderTexture
http://www.copypastecode.com/8403/And...
Now it is trivial to get a UIImage from a Texture2D...//This is a method I put into a Category to extend UIImage
+ (UIImage *) imageWithTexture2D:(Texture2D *)texture
{
int tx = texture.contentSize.width;
int ty = texture.contentSize.height;RenderTexture *renderer = [[RenderTexture alloc] initWithWidth:tx height:ty];
Sprite *tempSprite = [Sprite spriteWithTexture:texture];
tempSprite.anchorPoint = CGPointZero;[renderer begin];
[tempSprite visit];
[renderer end];return [renderer getUIImageFromBuffer];
}BTW, is it possible to get involved directly with the Cocos2D source and commit back to the repo?
Posted 2 years ago # -
@CJ - well done. If you look at the Google code page you can see there are a few committers for the cocos2d project so it is possible to get involved as a committer. However, in this circumstance I think the best approach would be to create a patch and either raise an issue or forward the patch to Jason Booth (Slipster216).
Posted 2 years ago # -
Yes, please commit this back to the repro. I meant to do this a while back and never got around to solving the problems. Riq can give you access, or I can merge the changes and submit it if you'd like..
Posted 2 years ago # -
Ok, I'll try to submit this back tonight..
Posted 2 years ago # -
thanks slipster216.
Posted 2 years ago #
Reply
You must log in to post.