I currently have an UIImage object which gets initialized with initWithFrame and then I have some 2D drawings in -(void)drawRect:(CGRect)rect{} with `CGContext. Very basic. All I want to know is, how can I draw through the push of e.g. a button or any event (not only on initialization)?
I tried copying the code inside drawRect to a new method called redraw but this results in an obvious error:
CGContextMoveToPoint: invalid context 0x0
I only want to know how to be able to draw through CGContext while the UIView has been created. Not e.g. how to listen for an UIButton event
Thank you.
You can only draw inside a UIView’s
drawRect:method or by creating your own image context. You cannot draw to the screen by any means other thandrawRect:or implementing drawing for a CALayer. You need to set variables on your view accordingly, then callsetNeedsDisplayto have it redrawn. For example, add a property@property(retain) UIImage *image;to your view, set the new image on it and then callsetNeedsDisplay(or even better, implement thesetImage:method and call[self setNeedsDisplay];here).