How can I make personal type material outside the elevator?

I have a UIView based class called Icon. 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 that I don’t want it inserted into drawRect, because it caused when my application makes each icon in a different view. How can I place this code elsewhere in the Icon class? I tried to put it in 1st place, and he 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 
+7
source share
4 answers

Keep doing all your drawings in -drawRect: When something changes and you want your view to be re-drawn, send him the message -setNeedsDisplay or -setNeedsDisplayInRect:

+5
source

Verify that the context size of the image is not invalid. If they, for example, {0,0}, you will get a NULL response that creates a context, and all calls that require a valid raster context will tell you that 0x0 is not a valid context.

+2
source

Perhaps you are looking for a UIGraphicsPushContext (context)? You can make your image context current to draw it anywhere UIKit is available. Do not forget UIGraphicsPopContext () after your drawing.

+1
source

How to add it to initWithFrame :? Will this work for you?

0
source

All Articles