1

現在、私のコードはアプリデリゲート、ビューコントローラー、およびメインビューでした。現在、背景として大きな画像を挿入しようとしていますが、どこに配置すればよいかわかりません。シミュレーション実行時のエラー

 <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)];

}
4

1 に答える 1

0

グラフィック コンテキストの外部で、「現在の」グラフィック コンテキストを必要とするメソッド ( [pic drawAtPoint:] ) を呼び出しています。UIKit は、drawRect: を呼び出す前に、バックグラウンドで自動的に作成します。

また、drawRect は、UIApplicationDelegate 内ではなく、MainView クラス内にある必要があります。

ループ内から [pic drawAtPoint:CGPointMake(160,0)] を削除してみてください。ループごとに描画する場合は、MainView クラス内の drawRect メソッド内で行う必要があります。

また、View Controller を使用しているとおっしゃいましたが、コードで参照されているものはありません。

ただし、画像を移動するだけの場合は、setNeedsDisplay を呼び出さないことを検討してください。むしろ、UIImageView を作成してビューに追加し、ループ メソッド内でその center プロパティを設定します。

于 2013-12-07T05:46:41.217 に答える