作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个名为 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 中,因为当我的应用程序在另一个 View 中制作每个图标时会调用它。我怎样才能将这种代码放在我的 Icon 类中的其他地方?我试着把它放在 1 个地方,它给了我:
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
最佳答案
继续在 -drawRect:
中绘制所有内容。当某些内容发生变化并且您希望重新绘制 View 时,向其发送 -setNeedsDisplay
或 -setNeedsDisplayInRect:
消息。
关于iphone - 我如何在 drawrect 之外做 drawrect 类型的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6049961/
我是一名优秀的程序员,十分优秀!