2013-04-24 98 views
0

我使用以前的答案中的以下代碼通過組合UIImageView和UITextView來創建新的UIImage。從UIImageView和UITextView創建UIImage

UIView *parentView = [[UIView alloc] initWithFrame:CGRectZero]; 
[parentView addSubview:self.imagePreview]; 
[self.imagePreview setFrame:CGRectMake(0, 0, 100, 100)]; 
[parentView addSubview:self.memeTextFieldTop]; 
[self.memeTextFieldTop setFrame:CGRectMake(20, 20, 80, 20)]; 
[parentView sizeToFit]; 

UIGraphicsBeginImageContext([parentView bounds].size); 
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

這個問題似乎是我目前收到以下錯誤,我不知道該如何去解決這些問題。

<Error>: CGContextSaveGState: invalid context 0x0 
<Error>: CGContextSetAlpha: invalid context 0x0 
<Error>: CGContextSaveGState: invalid context 0x0 
<Error>: CGContextGetBaseCTM: invalid context 0x0 
<Error>: CGContextConcatCTM: invalid context 0x0 
<Error>: CGContextSetBaseCTM: invalid context 0x0 
<Error>: CGContextSaveGState: invalid context 0x0 
<Error>: CGContextSetAlpha: invalid context 0x0 
<Error>: CGContextSaveGState: invalid context 0x0 
<Error>: CGContextConcatCTM: invalid context 0x0 

任何想法如何解決這個問題?

+0

我看你做了[parentView sizeToFit];你確定框架不是CGRectZero嗎? – 2013-04-24 11:35:54

+0

多數民衆贊成在'因爲parentView框架大小仍然爲零。用NSLog檢查框架。 – x4h1d 2013-04-24 11:46:20

回答

1

日誌顯示你沒有一個有效的上下文繪製

按照文檔

UIGraphicsGetCurrentContext()

當前的圖形上下文是默認爲零。在調用其 drawRect:方法之前,視圖對象會將有效上下文推送到堆棧上,使其變爲最新。但是,如果您未使用UIView對象執行繪圖,則必須使用UIGraphicsPushContext函數手動將有效上下文手動推入堆棧 。

通過用您創建的上下文調用UIGraphicsPushContext(),您的其他方法可以使用UIGraphicsGetCurrentContext()訪問該上下文。

如果你調用UIGraphicsGetCurrentContext()的drawRect之外:沒有與UIGraphicsPushContext()設定一個明確的背景下,當前的圖形上下文是不確定的,當然也不安全使用