Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i do drawrect type stuff outside of drawrect?

I have a UIView-based class called Icon. In there, I want to have code similar to this:

UIView *finalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
finalView.backgroundColor = [UIColor redColor];

UIGraphicsBeginImageContext(finalView.bounds.size);
[finalView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The problem is, i don't want to have to put it in drawRect because that's called when my app makes each Icon in another view. How can i put this sort of code somewhere else in my Icon class? I tried putting it in 1 place and it gave me:

CGContextSaveGState: invalid context 0x0
CGContextSetAlpha: invalid context 0x0
CGContextSaveGState: invalid context 0x0
CGContextSetFillColorWithColor: invalid context 0x0
CGContextAddRect: invalid context 0x0
CGContextDrawPath: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
like image 203
Andrew Avatar asked May 18 '11 19:05

Andrew


1 Answers

Keep doing all your drawing in -drawRect:. When something changes and you want your view to be re-drawn, sent it a -setNeedsDisplay or -setNeedsDisplayInRect: message.

like image 153
NSResponder Avatar answered Sep 19 '22 22:09

NSResponder