【问题标题】:random failure to load a texture from an image file从图像文件加载纹理的随机失败
【发布时间】:2014-11-17 19:59:13
【问题描述】:

我正在开发一个使用 GPUimage 将效果应用于实时视频的应用。

其中一个效果需要将图像作为纹理加载,然后该图像在视频上被过度播放。

所有图片都是PNG文件。用户可以从列表中选择一张图片。

我使用下面的代码来加载纹理,它在 98% 的时间内都能正常工作,但随机地纹理没有加载,我只是得到一个黑色方块。如果纹理无法加载并且我尝试使用它可以工作的相同图像重新加载它。再次加载失败是非常随机的。

任何想法/建议将不胜感激。

-(GLuint) loadTexture:(NSString *)fileName
{
    NSString *filePath =   [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],fileName];
    UIImage *image    = [UIImage imageWithContentsOfFile:filePath];

    GLuint width = image.size.width;
    GLuint height = image.size.height;

    if(width == 0 ){
        width = 64;
    }
    if(height == 0 )
    {
        height = 64;
    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    size_t cwidth = ceil( width );
    size_t cheight = ceil( height );
    size_t bytesPerRow = ( 4*cwidth + 15 ) & ~15;
    void *imageData = calloc( bytesPerRow, cheight );

    NSLog(@"texture : WIDTH %d HEIGHT %d\n", width, height);

    CGContextRef context = CGBitmapContextCreate( imageData, cwidth, cheight, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast |                                                                                                 kCGBitmapByteOrder32Big );

    CGColorSpaceRelease( colorSpace );

    CGContextClearRect( context, CGRectMake( 0, 0, cwidth, cheight ) );

    CGRect bounds=CGRectMake( 0, 0, cwidth, cheight );
    CGContextScaleCTM(context, 1, -1);
    bounds.size.height = bounds.size.height*-1;
    CGContextDrawImage(context, bounds, image.CGImage);


    GLuint lTextId;
    glGenTextures(1, &lTextId);
    glBindTexture(GL_TEXTURE_2D, lTextId);


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)cwidth, (GLsizei)cheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


    CGContextRelease(context);


    free(imageData);

    return( lTextId);
}

【问题讨论】:

    标签: ios xcode opengl-es gpuimage


    【解决方案1】:

    我不确定这会有所帮助,但我强烈建议您更改

    NSString *filePath =   [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],fileName];
    

    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    

    【讨论】:

    • 感谢您的建议,但不幸的是它没有解决问题
    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多