OpenGL ES does not display my UILabel layer as texture
I have this little problem and I think I am having it due to the lack of my experience in iOS development. Anyways, I am trying to display some preset text as a layer to my plane (one square). I am using OpenGL ES 1.1 and I chose to set UILabel and then to use its layer as my texture. Everything should be ok, but I am having some troubles. Here is how I get the layer:
- (void) convertTextToTexture:(NSString*)text toImage:(UIImage*)image {
UILabel *label = [[UILabel alloc] init];
CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:12]];
[label setBounds:CGRectMake(0, 0, size.width, size.height)];
[label setFrame:CGRectMake(0, 0, size.width, size.height)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:1];
label.text = text;
label.textColor = [UIColor redColor];
UIGraphicsBeginImageContext(label.frame.size);
[label.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[label release];
}
and I call it like this:
UIImage *image = [[UIImage alloc] initWithData:someData];
[self convertTextToTexture:@"TEXT" toImage:image];
The result is: I always get 开发者_JAVA百科someData
. If I just alloc
and init
, I get white texture. I think its a simple noobish mistake with using pointers and setting that info. Although in the debugger I noticed that UILabel
size is always 0x0
.
Thanks for your help and/or opinion.
Your image
is a local variable in this method, and gets thrown out when the method ends. I'm not sure exactly what you're trying to do, but it looks like image is something that this method should be returning:
- (UIImage *) convertTextToTexture:(NSString*)text {
// ...
return image;
}
And then you'd call it like:
UIImage * imageOfText = [self convertTextToTexture:@"TEXT"];
and use imageOfText
as desired.
(I say this without a 100% clear idea of what you're trying to load from data in this case. If you're generating the image inside the method, you shouldn't have anything to load in advance.)
精彩评论