开发者

Cocoa touch/Xcode - generating NIB-less graphics context

开发者 https://www.devze.com 2023-01-21 16:58 出处:网络
My app delegate contains: - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch

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 (i.e. UIImage).

0

精彩评论

暂无评论...
验证码 换一张
取 消