I am working on an app that applies effects to live video using GPUimage.
One of the effects required an image to be loaded as a texture and then that image is is overplayed on the video.
All of the images are PNG files. the used can select one of the images from the list.
I use the following code to load the texture and it works 98% of the time, but randomly the texture does not load and i just get a black square. If the texture fails to load and I attempt to reload it with the same image it works. Again this failure to load is very random.
Any thought/ suggestions would be appreciated.
-(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);
}
I am not sure it will help but I strongly suggest you to change
to