Ive been trying to work with UIGraphicsGetCurrentContext and CGContextRef. I was told that using UIGraphicsGetCurrentContext() numerous times is bad and to rather work on using CGContextRef and referring to it.
I have been trying to work on the second part and I am having issues setting the @property and referring to it. Can someone give me a declaration and usage example of this? Tried googling and couldn’t find any references to it.
Ta
You probably shouldn’t store the return value from
UIGraphicsGetCurrentContextin a property. You usually either don’t know how long the context will be valid, or the context has a short lifetime anyway. For example, if you’re callingUIGraphicsGetCurrentContextfrom yourdrawRect:method, you don’t know how long that context will survive after you return fromdrawRect:. If you’re callingUIGraphicsGetCurrentContextafter callingUIGraphicsBeginImageContextWithOptions, you will probably be callingUIGraphicsEndImageContextsoon anyway. It would be inappropriate to keep references to those contexts around.If you are calling many Core Graphics functions on the same context, then you want to store the context in a local variable. For example, here is a
drawRect:method from one of my test projects:You can see that I’m doing a bunch of stuff with the context: I’m saving and restoring the graphics state, I’m changing the CTM, and I’m drawing some Core Text frames into it. Instead of calling
UIGraphicsGetCurrentContextmany times, I call it just once and save the result in a local variable namedgc.