0

此函数在我的 GLKViewController 中,在创建 EAGLContext 并设置当前且视图加载之前调用:

-(GLint)prepareTextureWithImage:(UIImage *)image andName:(NSString *)name
{
    NSLog(@"[INFO] image has CGImage %@", image.CGImage);
    NSLog(@"[INFO] image has size %f, %f", image.size.width, image.size.height);

    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithBool:YES],
                              GLKTextureLoaderOriginBottomLeft,
                              nil];

    NSError *error;
    GLKTextureInfo *texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:options error:&error];
    if (error) {
        NSLog(@"[ERROR] Error loading texture from image: %@",error);
        return -1;
    }
    else {
        NSLog(@"[INFO] GlkitViewController prepared texture %@", texture);
        [textures setObject:texture forKey:name];
        return texture.name;
    }
}

这是从 Titanium 模块的 TiViewProxy 调用的,如下所示:

-(void)prepareTexture:(id)args
{
    ENSURE_UI_THREAD_1_ARG(args);
    ENSURE_SINGLE_ARG(args,NSDictionary);

    NSString *name = [TiUtils stringValue:[args objectForKey:@"name"]];

    TiBlob *blob = [args objectForKey:@"image"];
    UIImage *image = [blob image];
    [image retain];

    GlkitOverlayView *overlayView = (GlkitOverlayView *)view;
    GLKitViewController *vc = (GLKitViewController *)overlayView.viewController;

    NSLog(@"[INFO] using viewcontroller %@", vc);

    [vc prepareTextureWithImage:image andName:name];
}

我在控制台中得到了适当的输出:

[INFO]  using viewcontroller <GLKitViewController: 0xd5b68a0>
[INFO]  image has CGImage <CGImage 0xd5c6d40>
[INFO]  image has size 64.000000, 64.000000

但这是崩溃前的最后一个输出。

为什么对 GLKTextureLoader 的调用会崩溃?

4

1 回答 1

0

我不确定为什么同步调用会崩溃,但是我的函数的这个异步版本运行良好——纹理加载并在 glview 中正确显示。

-(GLint)prepareTextureWithImage:(UIImage *)image andName:(NSString *)name
{
    NSLog(@"[INFO] image has CGImage %@", image.CGImage);
    NSLog(@"[INFO] image has size %f, %f", image.size.width, image.size.height);

    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithBool:YES],
                              GLKTextureLoaderOriginBottomLeft,
                              nil];
    NSError *error;

    GLKTextureLoader *textureloader = [[GLKTextureLoader alloc] initWithSharegroup:self.context.sharegroup];
    GLKTextureInfo *myTexture;
    [textureloader textureWithCGImage:image.CGImage options:nil queue:nil completionHandler:^(GLKTextureInfo *textureInfo, NSError *error) {

        if(error) {
            NSLog(@"[ERROR] Error loading texture from image: %@",error);
        }
        else {
            NSLog(@"[INFO] GlkitViewController prepared texture %@", textureInfo);
            [textures setObject:textureInfo forKey:name];
        }
    }];
}
于 2013-04-28T02:10:38.490 回答