Reputation: 6239
I'm trying to draw a shape into an image using UIBezierPath. I do this in my viewDidLoad
, but I get a lots of invalid context
errors. What am I doing wrong?
If I move it in viewDidAppear
the drawing happens without any error but it isn't nice on the UI, since there's a delay between the appearance of the view and the image.
This is my function, called in 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;
I also tried with another approach, but it was unsuccessful in the same way (same error):
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;
These are the errors I get:
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
Upvotes: 1
Views: 3026
Reputation: 545
Try this code
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.
}
Hope this Helps!
Upvotes: 3
Reputation: 385510
I assume you are getting bounds
from some view. The viewDidLoad
and viewWillAppear:
methods are too early to look at a view's bounds, because the view hasn't been resized to fit the current screen yet.
You can look at your view's bounds
in viewDidLayoutSubviews
.
Upvotes: 4