2013-04-26 105 views
0

这个功能是我GLKViewController,一个EAGLContext已创建和的setCurrent和前视图加载之后叫:为什么这个调用GLKTextureLoader崩溃?

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

这正从一个钛模块的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崩溃?

回答

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