views:

50

answers:

1

I'm currently making a graphical application, and it will make my life a lot easier if I simply flip the context's transform matrix upside down whenever I want to draw something upside down, which is quite frequently. However, the only command I can find that supports this is CGContextScaleCTM. This is fine I guess, but it's an additive (well, multiplicative) process. If I scale it to 50% size, I then have to know that I've done that and enlarge it to 200% size after, which means retrieving the current matrix and doing a spot of mathematics every time I want to reset it to its original state of {1,0,0,1,0,0} which is also pretty darn often. There seem to be ways of doing this programmatically, but it seems extremely wasteful to spend processor time getting, fetching and multiplying matrices when all I really need to do is set the current transformation matrix to its absolute value.

However, I'm having great trouble discovering HOW, and it's really, really silly. I assume that the transformation matrix is stored somewhere in the CGContextRef structure as a CGAffineTransform, but I can't find a good document anywhere to tell me what its name is so that I can set it manually, and I don't even know if this would work. Can anyone help on this front?

-Ash

+1  A: 

I believe the only way to do what you are saying is what you are thinking of doing, that is using CGContextScaleCTM, however i don't see how the mathematics to check if you have it doubled/halved etc would be used that frequently if you only call them when you absolutely need to.

Update while typing: Have you tried using CGContextSaveGState and CGContextRestoreGState? I'm not exactly sure if its what you are looking for, but it would allow you to go back and forth between your halved/doubled i would think fairly easily?

Jesse Naugher
CGContextSaveGState() and CGContextRestoreGState() are almost certainly the right thing to do. That lets something call your code and apply a transformation. You can reset your transform with `CGAffineTransformConcatCTM(ctx, CGAffineTransformInvert (CGContextGetCTM(ctx))` (except it might not be exact due to rounding errors, and the transform might not be invertible), but it's a bit of a hack.
tc.
Thanks, that's very useful, I didn't know about the save/restore function. That will come in handy for a lot of other things too. You don't happen to know if it affects memory issues do you? I couldn't find any info in the documentation about whether the saved states are automatically removed from memory or not.
Ash