2

I was wondering how to draw a CGRect onto an UIImageView. Here is the code I've got but it doesn't seem to be working. Any suggestions? touch1 and touch2 are both CGPoints and point is a CGRect.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch1 = [touch locationInView:self];
touch2 = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch2 = [touch locationInView:self];
point = CGRectMake(touch2.x, touch2.y, 50, 50);
[self setNeedsDisplay];
}

 - (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, pointColor.CGColor);
CGContextAddEllipseInRect(context, point);
CGContextDrawPath(context, kCGPathFillStroke);
}
Brandon
  • 127
  • 4
  • 10
  • 1
    are you trying to draw a rectangle or a circle? You said in your comments 'a CGRect' but in the code it says CGContextAddEllipseInRect – slf Nov 22 '10 at 19:10
  • sorry for not specifying…at this point I'm just trying to get something to draw on the screen in general. I have a class that is a subclass of a UIViewController which has the imageView displaying an actual image. I want to be able to draw on that image only. – Brandon Nov 22 '10 at 19:15

3 Answers3

1

What do you see actually happening?

A few notes:

  1. You shouldn't be doing this with a UIImageView-- those are specifically intended to be containers for image files. You should just use a subclass of a regular UIView for this.
  2. In your touchesBegan, you know that touch1 and touch2 will always be set to the same thing, right? You don't seem to ever be using touch1.
  3. point is a misleading variable name for something that is a rect.
  4. Your drawRect is not unreasonable. What is pointColor? If you're drawing black-on-black that might be part of the problem.
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • I really only need 1 touch for right now. I eventually, after drawing it on the screen, want to be able to drag that "point" around to move it wherever I want. "point" may be a little misleading to you as someone who isn't using this program. pointColor is a UIColor and I've tried to draw it on the screen in general which has a white background with a picture loaded into a UIImageView. – Brandon Nov 22 '10 at 19:17
0

Listing 3-1 Code that creates an ellipse by applying a transform to a circle

CGContextScaleCTM(context, 1,2);
CGContextBeginPath(context);
CGContextAddArc(context, 0, 0, 25, 0, 2*M_PI, false);
CGContextStrokePath(context);

Lots more example code in the Quartz2d Example

slf
  • 22,595
  • 11
  • 77
  • 101
0

I took your code only, its working for me. Just have look at it. And thanks for your code. In this sample i am drawing a rect.

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();  
    CGContextSetLineWidth(context, 2.0);  
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);  
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
    CGContextAddRect(context, rectFrame);  
    CGContextDrawPath(context, kCGPathFillStroke);  

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self];
    rectFrame.origin.x = startPoint.x;
    rectFrame.origin.y = startPoint.y;
}  
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }
Naveen Thunga
  • 3,675
  • 2
  • 24
  • 31