2013-07-12 19 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. 
} 

希望這有助於!

相關問題