我正在尝试在
根据我的理解,这意味着我试图在一个空的上下文中使用
尝试进行如下操作:
在绘制图像之前没有任何改变。我该如何克服这个问题?多线程对于响应性是必须的,但上下文在这个过程中丢失了。
更新:以下是我的代码:
UIView
中手动使用drawRect:
绘制几个UIImage
,这些图片是从网上动态下载的,因此我创建了一个NSOperation
并从第二个线程执行图像加载代码,以保持UI响应性。当图片下载完成后,它们将显示在屏幕上。但是...我为每个图像都收到以下错误提示:<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
根据我的理解,这意味着我试图在一个空的上下文中使用
[image drawInRect...]
,因为我不再在drawRect:
中了。尝试进行如下操作:
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
在绘制图像之前没有任何改变。我该如何克服这个问题?多线程对于响应性是必须的,但上下文在这个过程中丢失了。
更新:以下是我的代码:
- (void)drawContentView:(CGRect)r
{
CGContextRef context = UIGraphicsGetCurrentContext();
....
CGContextFillRect(context, r);
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(loadPreview)
object:nil];
[queue addOperation:operation];
[operation release];
}
然后
-(void)loadPreview
{
self.preview = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:self.picStringPreview]]];
[self.preview drawInRect:CGRectMake(256, 18, 80, 65)];
}
我尝试在loadPreview
中添加UIGraphicsPushContext...
,甚至在那里执行[self performSelectorOnMainThread...]
,但没有任何效果。而且这是基于Atebit模型的自定义单元格,所以我的单元格超类执行了:
- (void)drawRect:(CGRect)r
{
[(ABTableViewCell *)[self superview] drawContentView:r];
}
在drawContentView:
中的代码将会被绘制在父类的drawRect:
中。有什么提示吗?
drawRect:
方法的片段吗?你是在drawRect:
中创建NSOperation
然后尝试从另一个线程中绘制图像吗? - jscs