2015-10-25 3 views
0

Я пытаюсь читать данные текстуры, используя CIImage initWithTexture под iOS 9.0, но все, что я получаю, это черное изображение. Я даже попытался сначала загрузить текстуру, а затем прочитать ее, но все еще черное изображение.Создание CIImage с initWithTexture

Вот мой код:

CGImageRef brushImage; 
    CGContextRef brushContext; 
    GLubyte *brushData; 
    size_t width, height; 
    GLUint texId; 
    CIImage *cimage; 

    // 
    // Here I'm just loading an image and preparing it as a bitmap. 
    // 
    brushImage = [UIImage imageNamed:"brush"].CGImage; 
    width = CGImageGetWidth(brushImage); 
    height = CGImageGetHeight(brushImage); 
    brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte)); 
    brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast); 
    CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage); 


    // 
    // Here I create the texture I want to use. 
    // 
    glGenTextures(1, &texId); 
    glBindTexture(GL_TEXTURE_2D, texId); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

    // 
    // Load in my bitmap to the texture. 
    // 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)width, (int)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData); 

    // 
    // Now, try to read the texture back out. 
    // 
    cimage = [[CIImage alloc] initWithTexture:texId size:CGSizeMake(width, height) flipped:YES colorSpace:CGImageGetColorSpace(brushImage)]; 

    return [[UIImage alloc] initWithCIImage: cimage]; 

Возвращение UIImage, кажется, правильного размера - но изображение, кажется, пустые (все черные), хотя исходное изображение (а PNG) определенно имеет некоторое содержание в Это. Не уверен, что я делаю неправильно. Любая помощь, пожалуйста? Благодаря!!

ответ

2

Я был в состоянии сделать эту работу, используя GLKTextureLoader из GLKit, я создал следующий метод для создания текстуры резервная копия CIImage из образа:

- (CIImage *)createImageFromPath:(NSString *)filePath{ 
    NSError *error; 
    GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:filePath options:@{GLKTextureLoaderApplyPremultiplication : @YES} error:&error]; 
    glBindTexture(texture.target, texture.name); 

    return [CIImage imageWithTexture:texture.name size:CGSizeMake(texture.width, texture.height) flipped:YES colorSpace:nil]; 
} 

Для того, чтобы это работало, вам необходимо убедиться, что вы перед вызовом этой функции необходимо установить EAGLContext. Кроме того, необходимо создать CIContext основанный на том же EAGLContext оказывать CIImage

EAGLContext *eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 
CIContext *ciContext = [CIContext contextWithEAGLContext:eaglContext options:@{kCIContextWorkingColorSpace : [NSNull null]}]; 
[EAGLContext setCurrentContext:eaglContext]; 

CIImage *image = [self createImageFromPath:[[NSBundle mainBundle] pathForResource:@"brush" ofType:@"png"]]; 

Вы можете применить фильтры к этому CIImage и сделать изображение с помощью ciContext.