2013-07-12 18 views
1

我试图使用UIBezierPath将图形绘制到图像中。我在我的viewDidLoad中这样做,但是我收到了很多invalid context错误。我究竟做错了什么?使用UIBezierPath在viewDidLoad中绘制图像:无效的上下文错误

如果我将它移动到viewDidAppear中,绘图发生时没有任何错误,但由于视图外观和图像之间存在延迟,所以在UI上不太好。

这是我的功能,在viewDidLoad中调用。

UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0.0f); 

UIBezierPath* buttonBackgroundPath = [UIBezierPath bezierPath]; 
[buttonBackgroundPath moveToPoint: CGPointMake(16.81, 1.02)]; 
[buttonBackgroundPath addLineToPoint: CGPointMake(size.width, 1.02)]; 
[…] 
[buttonBackgroundPath addLineToPoint: CGPointMake(10.66, 6.2)]; 
[buttonBackgroundPath closePath]; 

[buttonBackgroundPath fill]; 
[strokeColor setStroke]; 
buttonBackgroundPath.lineWidth = 1; 
[buttonBackgroundPath stroke];*/ 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return image; 

我也试图与另一种方法,但它以同样的方式失败(同样的错误):

UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0.0f); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextMoveToPoint(context, 16.81, 1.02); 
[…] 
CGContextAddLineToPoint(context, 10.66, 6.2); 
CGContextClosePath(context); 

CGContextSetLineCap(context, kCGLineCapRound); 
CGContextSetLineWidth(context, 1.0); 
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); 
CGContextStrokePath(context); 
CGContextSetFillColorWithColor(context, fillColor.CGColor); 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return image; 

这是我得到的错误:

Jul 13 01:46:29 <Error>: CGContextSetLineCap: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextSetLineWidth: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextDrawPath: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextSetFillColorWithColor: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextMoveToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddLineToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddLineToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddLineToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddCurveToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddCurveToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddLineToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddLineToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextAddCurveToPoint: invalid context 0x0 
Jul 13 01:46:29 <Error>: CGContextClosePath: invalid context 0x0 
+0

请从调试控制台复制**整个**输出并将其粘贴到您的问题中。 –

+0

@robmayoff当然,对不起! – entropid

+0

@KHansenSF好吧,我在堆栈溢出时通过了关于此问题的所有问答,但没有运气。正如你所看到的那样,我在第二个答案中做了什么,但我无法实现它的工作。 :/ – entropid

回答

4

我承担你从某种角度得到boundsviewDidLoadviewWillAppear:方法对于视图的边界来说太早了,因为视图还没有调整到适合当前屏幕。

您可以在viewDidLayoutSubviews中查看您的视图bounds

+0

这就是问题所在!我不明白我以前没有意识到。它完美无缺地工作,非常感谢你。 :)) – entropid

3

试试这个代码

ViewController.h 

#import 
@interface ViewController : UIViewController 
{ 
UIImageView *drawpad; 
} 
@property (retain, nonatomic) IBOutlet UIImageView *drawpad; 
@end 

ViewController.m

@implementation ViewController 
@synthesize drawpad; 
- (void)viewDidLoad 
{ 
UIBezierPath *path = [[UIBezierPath alloc] init]; 
[path moveToPoint:CGPointMake(50.0f, 50.0f)]; 
[path addLineToPoint:CGPointMake(270.0f, 50.0f)]; 
[path addLineToPoint:CGPointMake(270.0f, 500.0f)]; 
[path addLineToPoint:CGPointMake(50.0f, 500.0f)]; 
[path closePath]; 
UIGraphicsBeginImageContext(self.view.frame.size); 
path.lineCapStyle = kCGLineCapRound; 
path.lineWidth = 10.0f; 
[[UIColor blackColor] setStroke]; 
[[UIColor redColor] setFill]; 
[path stroke]; 
[path fill]; 
self.drawpad.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

希望这有助于!

相关问题