2

I'm trying to write a finger paint type program using Quartz graphics. The problem I'm having is when the screen rotates. The dots and lines I draw "creep" away from where I'm touching. With each successive touch, the previous dot creeps away. This does not happen in either of the two portrait modes, but it does in both landscape modes.

I may be mistaken but I think my problem is with this call:

[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];

drawImage is a UIImageView in my main view controller's nib file.

I've also tried using the following:

[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height  )];

[drawImage.image drawInRect:drawImage.bounds];

[drawImage.image drawInRect:self.view.bounds];

I guess I really have no idea whether to use self or my UIImageView object for this call.

I have tried both, in both the drawInRect call and where I set up my drawing context (being consistent).

I set up the context like this in viewDidLoad (to try to make a persistant context that I don't have to reset things in each time I draw - using push and pop context).

//---------------- set up a persistent context for drawing -----------------

UIGraphicsBeginImageContext(self.view.frame.size);
context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, curWidth);
CGContextSetRGBStrokeColor(context, curRed, curGreen, curBlue, curAlpha);
UIGraphicsPushContext(context);

I tried to get rid of the context I set up on rotation and reinitialize it when the view rotated, thinking this was the problem, but I'm not sure I did it right.

jscs
  • 63,694
  • 13
  • 151
  • 195
Rich
  • 41
  • 3
  • Where are you calling `drawInRect:` from? Generally speaking, you shouldn't need to set up your own drawing context. UIKit sets one up for you before it asks your objects to draw. It also seems strange to be explicitly drawing the image of a UIImageView; that's what the view is for, after all. I'm not 100% sure what's going on, but it looks like you're having trouble because you're trying to do things outside the way that UIKit wants you to do them. – jscs May 30 '11 at 02:03
  • Thanks for your response Josh. I am pretty sure you're right, that I'm trying to do things outside of UIKit norms but it's not because I want that way, it's inexperience! Being new, I'm a bit of a copy-and-paste programmer at this point. I started with some code I found that draws to a UIImageView object from within the touchesBegan, touchesMoved and touchesEnded methods. It is in those methods that I'm calling drawInRect to draw what's in the UIImageView to the current context. I then modify the context and draw it back to the UIImageView. I may have a solution for my problem. continued – Rich May 30 '11 at 02:13
  • Continued... I am now using drawAtPoint and passing it a point of 0,0. This gets me around the whole size mismatch thing. I also found that by using the UIImageView object to set up my context, I no longer need to adjust the touch coordinates to match my UIImageView object. I'm now setting the context up like this: UIGraphicsBeginImageContext(drawImage.frame.size); – Rich May 30 '11 at 02:20

1 Answers1

0

You shouldn't be drawing that image yourself directly. Even if you set up your own context and draw into it correctly, you've got to somehow get it flushed to the screen. This is why UIKit sets up a context for you before it calls drawRect: on views. It's ready to put things on the screen and it just needs to know what to put there.

You can set up your own image context and draw into it in touchesBegan:, etc., but what you should do then is turn that into an image using UIGraphicsGetImageFromCurrentImageContext, call [myImageView setImage:theImageIJustGot], and then [myImageView setNeedsDisplay]. That signals UIKit that you have new things to put on the screen, and UIKit will take care of the rest.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Rich, let me know if that doesn't make sense or if you have more questions. – jscs May 30 '11 at 02:31
  • Thanks Josh, that helped a lot. Some of that I think I'm doing in a round about way. I'll rework my code to make it more as you suggest. Besides structural problems you point out, I think my immediate symptom was caused by passing the wrong sizes when moving images to and from the graphics context. Thanks Again! – Rich May 30 '11 at 04:08