OpenGL ES 不将我的 UILabel 图层显示为纹理

发布于 2024-11-15 00:02:00 字数 1300 浏览 4 评论 0原文

我有这个小问题,我想我之所以遇到这个问题是因为我缺乏 iOS 开发经验。无论如何,我正在尝试将一些预设文本显示为我的平面(一个正方形)的一层。我使用的是 OpenGL ES 1.1,我选择设置 UILabel,然后使用它的图层作为我的纹理。一切都应该没问题,但我遇到了一些麻烦。以下是我获取图层的方式:

- (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]; 
}

我这样称呼它:

UIImage *image = [[UIImage alloc] initWithData:someData];

[self convertTextToTexture:@"TEXT" toImage:image];

结果是:我总是得到 someData。如果我只是 allocinit,我会得到白色纹理。我认为使用指针和设置该信息是一个简单的新手错误。尽管在调试器中我注意到 UILabel 大小始终为 0x0

感谢您的帮助和/或意见。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-11-22 00:02:00

您的 image 是此方法中的局部变量,并在方法结束时被抛出。我不确定你到底想做什么,但看起来图像是这个方法应该返回的东西:

- (UIImage *) convertTextToTexture:(NSString*)text {
     // ...
     return image;
}

然后你可以这样称呼它:

UIImage * imageOfText = [self convertTextToTexture:@"TEXT"];

并使用 imageOfText 根据需要。

(在这种情况下,我并没有 100% 清楚地了解您要从数据中加载什么内容,才这么说。如果您在方法内生成图像,则不应该提前加载任何内容。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文