我試圖在UIView
中繪製幾個UIImage
,我正在用drawRect:
手動執行。鑑於圖像是從網上動態下載的,我創建了一個NSOperation
,並從第二個線程執行圖像加載代碼,以保持UI響應。所以一旦圖像被下載,它們就會出現在屏幕上。上下文和線程在drawRect之間的問題
但是...我遇到下面的錯誤,每個圖像的:
<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...]
是nil
,因爲我現在不在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:
被draw'd。任何提示?
你可以發佈一個'drawRect:'方法的片段嗎?你是在創建'NSOperation' _inside_'drawRect:',然後試圖從其他線程中繪製圖像? – 2011-05-17 02:51:17
我現在不在家,只要我回來,我會用片段更新我的問題。謝謝! – ferostar 2011-05-17 12:50:44
只是更新後的代碼片段 – ferostar 2011-05-18 01:24:46