我試圖使用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
請從調試控制檯複製**整個**輸出並將其粘貼到您的問題中。 –
@robmayoff當然,對不起! – entropid
@KHansenSF好吧,我在堆棧溢出時通過了關於此問題的所有問答,但沒有運氣。正如你所看到的那樣,我在第二個答案中做了什麼,但我無法實現它的工作。 :/ – entropid