Cocoa touch/Xcode - generating NIB-less graphics context

大城市里の小女人 提交于 2020-01-16 05:15:10

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/4016621/cocoa-touch-xcode-generating-nib-less-graphics-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!