出于某种原因,我的变量文件名在从最近的类实例到setupView到filename时发生了变化,我无法弄清楚原因。我刚开始学习obj-C,所以我假设我正在做某种新手的错误。
它们通过以下方式实例化:
campusMapTileZero=[[CampusMapTile alloc] initNameOfTiletoLoad:@"cm0"];
campusMapTileOne=[[CampusMapTile alloc] initNameOfTiletoLoad:@"cm1"];
和我所指的实现是:
#import "CampusMapTile.h"
#import "TileManager.h"
#import "GLView.h"
#import "OpenGLCommon.h"
#import "ConstantsAndMacros.h"
@implementation CampusMapTile
NSString *filename;
- (id) initNameOfTiletoLoad: (NSString *)filename
{
if(self = [super init]) {
NSLog(@"initialized map tile for tile:");
NSLog(filename);
[self setTile:filename];
}
return self;
}
-(void)setTile:(NSString*) _filename {
NSLog(@"set tile called from setTile.");
filename=_filename;
NSLog(@"filename in setTile is:");
NSLog(filename);
}
- (void)drawView:(GLView*)view
{
glBindTexture(GL_TEXTURE_2D, texture[0]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
static const Vertex3D vertices[] = {
{0,0, 1}, //TL
{ 1024,0, 1}, //TR
{0,-1024, 1}, //BL
{ 1024.0f, -1024.0f, 1} //BR
};
static const GLfloat texCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
- (void)setupView:(GLView*)view {
NSLog(@"setup view called");
NSLog(filename);
[self loadTextures:filename textureIdentifier:0];
}
-(void)loadTextures:(NSString*) filename textureIdentifier:(int) textureNumber {
//enable textures.
glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Bind the number of textures we need.
glGenTextures(1, &texture[textureNumber]);
glBindTexture(GL_TEXTURE_2D, texture[textureNumber]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// [tileManager setupView:view];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"jpg"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil)
NSLog(@"Do real error checking here");
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
// Flip the Y-axis
CGContextTranslateCTM (context, 0, height);
CGContextScaleCTM (context, 1.0, -1.0);
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
CGContextRelease(context);
free(imageData);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
答案 0 :(得分:2)
问题是你的filename
变量是全局变量,而不是实例变量。
您需要删除现有的NSString* filename
声明,并在您的课程filename
块中声明@interface
,如下所示:
@interface CampusMapTile : UIViewController {
NSString* filename;
}
答案 1 :(得分:1)
在setTile
中你只是指定指针,所以如果指针指向的字符串发生变化,你存储在类实例中的filename
也会改变。尝试类似的事情:
-(void)setTile:(NSString*) _filename {
NSLog(@"set tile called from setTile.");
filename = [_filename copy];
NSLog(@"filename in setTile is:");
NSLog(filename);
}