简体   繁体   中英

Cocoa touch/Xcode - generating NIB-less graphics context

My app delegate contains:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  // Override point for customization after app launch    
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  if (!window) 
  {
    [self release];
    return;
  }

  window.backgroundColor = [UIColor greenColor];
  [window addSubview:viewController.view];
  [window makeKeyAndVisible];
  [window layoutSubviews];
}

This does get executed, apparently with no errors. The window variable is NOT 0 so the test if (! window) does not cause the function to return. However no green-background window appears, just the default color. And in the appController.m file, code in the viewDidLoad method does execute. However CGContextRef context = UIGraphicsGetCurrentContext(); returns null rather than a real context.

What am I leaving out?

I suspect your window is not green because the view you're adding with [window addSubview:viewController.view]; is opaque and covering the entire window.

As to the other problem, if you're asking for the current graphics context in viewDidLoad , I don't think there is guaranteed to be one. You can only draw on UIViews by subclassing and overriding their drawRect:(CGRect) method. If you want to draw outside these, you'll need to create your own graphics context via something like CGBitmapContextCreate , then display the results either by drawing them in a view's drawRect: or by finding another control that will take a CGImage you've made with the bitmap functions (ie UIImage ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM