【发布时间】:2011-05-18 19:23:30
【问题描述】:
我有一个名为 Icon 的基于 UIView 的类。在那里,我想有类似这样的代码:
UIView *finalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
finalView.backgroundColor = [UIColor redColor];
UIGraphicsBeginImageContext(finalView.bounds.size);
[finalView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
问题是,我不想将它放在 drawRect 中,因为当我的应用程序在另一个视图中创建每个图标时会调用它。我怎样才能将这种代码放在我的 Icon 类的其他地方?我试着把它放在一个地方,它给了我:
CGContextSaveGState: invalid context 0x0
CGContextSetAlpha: invalid context 0x0
CGContextSaveGState: invalid context 0x0
CGContextSetFillColorWithColor: invalid context 0x0
CGContextAddRect: invalid context 0x0
CGContextDrawPath: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
【问题讨论】:
-
我明白了,你想渲染一次,然后显示结果?
-
是的,我想先显示视图,然后再将其更改为不同的视图。
-
如果将上下文放入变量
CGContextRef currCtx = UIGraphicsGetCurrentContext();和NSLog它会得到什么? -
所以,您还没有成功创建上下文。
finalView.bounds.size的值是多少?
标签: iphone objective-c ios graphics core-graphics