【问题标题】:Drawing an image with UIBezierPath in viewDidLoad: invalid context error在 viewDidLoad 中使用 UIBezierPath 绘制图像:无效的上下文错误
【发布时间】:2013-07-12 22:56:05
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 请从调试控制台复制整个输出并将其粘贴到您的问题中。
  • @robmayoff 当然,对不起!
  • @KHansenSF 好吧,我在 Stack Overflow 上完成了所有关于此事的问答,但没有运气。正如你所看到的,我做了第二个答案中所说的,但我无法让它工作。 ://
  • 我假设您使用的是自动布局?

标签: ios uiimage cgcontext uibezierpath


【解决方案1】:

我假设您从某种角度得到boundsviewDidLoadviewWillAppear: 方法对于查看视图的边界还为时过早,因为尚未调整视图的大小以适应当前屏幕。

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

【讨论】:

  • 这就是问题所在!我不明白我以前怎么没有意识到这一点。它完美无缺,非常感谢。 :))
【解决方案2】:

试试这个代码

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.
}

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多