2013-04-24 47 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()设定一个明确的背景下,当前的图形上下文是不确定的,当然也不安全使用