Question

Right now my code was an app delegate, a view controller, and a main view right now i'm just trying to insert a large image as the background, but i can't figure out where to put it and i'm getting an error when i run a simulation

 <Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.


- (void) applicationDidBecomeActive: (UIApplication *) application {
    NSLog(@"AAAAAAAAAA");
    [self startGameLoop];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"AAAAA");
    mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
    mainView.backgroundColor = [UIColor grayColor];
    [self.window addSubview: mainView];
    [self.window makeKeyAndVisible];
    [self startGameLoop];
    return YES;
}



    - (void) startGameLoop {
    quack = [NSTimer scheduledTimerWithTimeInterval:.0303 target:self selector:@selector(loop)       userInfo:nil repeats:YES];
    NSLog(@"Game Loop timer instance: %@", quack);
    mainView.backgroundColor = [UIColor grayColor];
}

- (void) loop {
    NSLog(@"BBBBBB");
    pic = [UIImage imageNamed:@"f1.png"];
    [pic drawAtPoint:CGPointMake(160, 0)];
    [mainView setNeedsDisplay];

}

- (void) drawRect: (CGRect) rect {
    maps = [UIImage imageNamed:@"map.png"];
    [maps drawAtPoint:CGPointMake(W/2, 0)];

}
Was it helpful?

Solution

You're calling a method ( [pic drawAtPoint:] ) that requires a "current" graphics context, outside of a graphics context. UIKit automatically creates one behind the scenes before any call to drawRect:.

Also, your drawRect must be in the MainView class, not within the UIApplicationDelegate.

Try removing [pic drawAtPoint:CGPointMake(160,0)] from within loop. If you want to draw it every loop, it must be done within the drawRect method, within the MainView class.

Also, you mentioned you're using a view controller but I see none referenced in your code.

But if you only want to move an image around, you may want to consider NOT calling setNeedsDisplay. Rather, create a UIImageView, add it to your view, and setting its center property within the loop method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top