1

我正在尝试使用 UIBezierPath 将形状绘制到图像中。我在我的 . 中这样做viewDidLoad,但我得到了很多invalid context错误。我究竟做错了什么?

如果我在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
4

2 回答 2

4

我假设你是从某种角度bounds来看的。和方法查看视图边界viewDidLoadviewWillAppear:为时过早,因为视图尚未调整大小以适应当前屏幕。

您可以查看您的视图boundsviewDidLayoutSubviews

于 2013-07-13T00:47:53.880 回答
3

试试这个代码

ViewController.h

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

视图控制器.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.
}

希望这可以帮助!

于 2013-08-14T11:58:30.357 回答