I´m trying to draw a circle with a number inside of it:
-(void)drawRect:(CGRect)rect {
//Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, rect);
CGContextDrawPath(context, kCGPathFillStroke);
// Label and index code
UILabel* circleLabel = [[UILabel alloc] init];
NSString *i=[Index stringValue];
[circleLabel setText:i];
[self addSubview:circleLabel];
}
I call the drawRect method from my own method:
-(void)addPhotoIndex:(NSNumber*)tempNumber withCoordinate:(NSString*)currentTouchPos{
self.coord= CGPointFromString(currentTouchPos);
CGRect rect= CGRectMake ((coord.x - 5), (coord.y- 5), 5, 5);
NSNumber *Index= tempNumber;
[self drawRect:rect];
}
That it is itself called from the ViewController.
But this is not drawing nothing, nor giving any errors. Can somebody help me please?
SOLUTION:
At the end, this is what it worked:
-(void)addPhotoIndex:(NSNumber*)tempNumber withCoordinate:(NSString*)currentTouchPos{
self.coord= CGPointFromString(currentTouchPos);
CGRect rect= CGRectMake ((coord.x - 3), (coord.y- 3), 3, 3);
NSNumber *Index= tempNumber;
NSString *i=[Index stringValue];
UIFont *font = [UIFont systemFontOfSize:15.0];
// Drawing Point
UIGraphicsBeginImageContext(self.frame.size);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, rect);
CGContextDrawPath(context, kCGPathFillStroke);
[ i drawAtPoint:self.coord withFont:font];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
A point with the index number are drawn next to each other in the correct position :) Thanks for your useful help!
The method you should be using is drawRect:
not drawInRect:
.
You also do not need to create your own context as this is set up for you in drawRect:
- (void)drawRect:(CGRect)rect;
{
// UIGraphicsBeginImageContext(self.bounds.size); <-- not needed
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, rect);
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill
/*
* As @Jonathan Cichon points out you should not add subview's here
* either add them elsewhere or draw the string manually
*/
// UILabel* circleLabel = [[UILabel alloc] init];
// NSString *i=[Index stringValue];
// [circleLabel setText:i];
// [self addSubview:circleLabel];
// UIGraphicsEndImageContext(); <-- not needed
}
I assume you implementated - (void)drawInRect:(CGRect)rect
in a subclass of UIView
. If so use - (void)drawRect:(CGRect)rect
, so the context for drawing is already allocated when your method is called. Remove the UIGraphicsBeginImageContext
and UIGraphicsEndImageContext
calles. Also you should not add a subview in drawRect
, use [i drawInRect:withFont:]
instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With