Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法确定此目标是什么,c访问错误_Ios_Objective C_Opengl Es - Fatal编程技术网

Ios 无法确定此目标是什么,c访问错误

Ios 无法确定此目标是什么,c访问错误,ios,objective-c,opengl-es,Ios,Objective C,Opengl Es,我直接从一本书中复制了这段代码,但我不知道问题出在哪里。在调用glTexImage2d的最后一行中,我遇到了EXC错误访问运行时错误。我想它来自像素数据指针。但我不知道问题出在哪里。如果有天才能帮忙的话,我会非常感激的。我使用的是Xcode 6 #import "TextureHelper.h" @implementation TextureHelper -(void) createTextureFromImage: (NSString *) picName{ UIImage

我直接从一本书中复制了这段代码,但我不知道问题出在哪里。在调用glTexImage2d的最后一行中,我遇到了EXC错误访问运行时错误。我想它来自像素数据指针。但我不知道问题出在哪里。如果有天才能帮忙的话,我会非常感激的。我使用的是Xcode 6

#import "TextureHelper.h"



@implementation TextureHelper


-(void) createTextureFromImage: (NSString *) picName{
    UIImage *pic = [UIImage imageNamed:picName];

    int scaleFactor = 1;

    float iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

    if(iOSVersion >= 4.0){
        if(pic.scale >= 2){
            scaleFactor = pic.scale;
        }
    }
    if(pic){
        //set texture dimentions
        width = pic.size.width * scaleFactor;
        height = pic.size.height * scaleFactor;
        /*
        if ((width & (width-1)) !=0 || (height & height-1)) != 0 || width> 2048 || height > 2048) {
            NSLog(@"ERROR:%@ width and/or height is not power of 2 or is > 2048!", picName);
        }
        */

        GLubyte *pixelData = [self generatePixelDataFromImage:pic];
        [self generateTexture:pixelData];

        int memory = width*height*4;
        NSLog(@"%@, Size:%i KB, ID:%i", picName, memory/1024, textureID);
        free(pixelData);

    }else{
        NSLog(@"ERROR:%@ not found, texture not created.",picName);
    }
}

-(GLubyte *) generatePixelDataFromImage: (UIImage *) pic{

    GLubyte *pixelData = (GLubyte *) calloc(width * height *4, sizeof(GLubyte));
    CGColorSpaceRef imageCS = CGImageGetColorSpace(pic.CGImage);

    CGBitmapInfo bitmapInfo = (CGBitmapInfo) kCGImageAlphaPremultipliedLast;
    CGContextRef gc = CGBitmapContextCreate(pixelData, width, height, 8, width*4, imageCS, bitmapInfo);

    CGContextDrawImage(gc, CGRectMake(0, 0, width, height), pic.CGImage);
    CGContextRelease(gc);
    return pixelData;
}

-(void) generateTexture: (GLubyte * ) pixelData{
    //Create GL Texture
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

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

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixelData);

}

@end

我修复了这个问题,因为我愚蠢地在pixelData中包含了&符号