CGContext - is there a way to reset the current context - iphone

In my iPhone project, I've got a UIView where I implement the drawRect method:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
Inside the method I do a whole bunch of drawing of lines, images and texts using this context. The problem is that when I re-use this view, the context does not get reset. Is there a method I can call to reset the context somehow?

You might want to check out CGContextSaveGState(context) and CGContextRestoreGState(context). They will let you push and pop the current state of the context.
If your view is getting 'mangled', check your view's contentMode setting.

You can also use CGContextClearRect(context, CGRectMake(0, 0, width, height));

Related

How to get iphone device screenshot when image animation is run on main thread?

I am perfoming image sequences animation is on main thread.
At the same time i want to take snapshot of device screen in back ground.
And by using that snapshots i want make video..
Thanks,
Keyur Prajapati
For taking the screenshots while running the image animations
use
[self.view.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
instead of
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
it will take screenshots while runing animations
iOS uses Core Animation as the rendering system. Each UIView is backed by a CALayer. Each visible layer tree is backed by a presentation tree. The layer tree contains the object model values for each layer, i.e., values you set when you assign a value to a layer property (A and B). The presentation tree contains the values that are currently being presented to the user as an animation takes place (interpolated values between A and B).
If you're doing it in CoreAnimation you can render the layer contents into a bitmap using -renderInContext:. Have a look at Matt Longs Tutorial. It's for Objective-C on the Mac, but it can be easily converted for use on the iPhone.
Create one another thread where you can do this:
//Create rect portion for image if full screen then 320X480
CGRect contextRect = CGRectMake(0, 0, 320, 480);
// this is whate you need
UIGraphicsBeginImageContext(contextRect.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
viewImage is the image which you needed.
You can write this code in function which can be called timely bases like per 5 seconds or according to your requirements.
Hope this is what you needed.

iOS Quartz UIView integration

I'm writing an iPhone app and I'm new to the iOS sdk. Heres what I'm trying to accomplish. I want an image to flash on top of my screen (On top meaning over everything that's already in my view). I could do this fine, but I want it to be semi-transparent so I'm assuming I need to use the quartz libraries.
I implemented these two functions in a UIView class. This code that works ok but it creates a black background around the image. I want just the image with no box background around it. Is there a CGContext function I can use to alter or remove the background? Should I not be calling drawRect? Am going completely in the wrong direction? Let me know if I anyone needs more information. Thanks.
// Implements UIView
-(void)drawInContextCGContextRef)context{
CGRect imageRect;
imageRect.origin = CGPointMake(8.0, 8.0);
imageRect.size = CGSizeMake(64.0, 64.0);
CGContextSetAlpha(context, 0.5);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, imageRect, image);
}
-(void)drawRectCGRect)rect
{
[self drawInContext:UIGraphicsGetCurrentContext()];
}
Here's my code that calls it from the view controller.
[quartzUIView drawRect:CGRectMake(50, 50, 150, 250)];
[self.view addSubview:quartzUIView];
I think you're over-complicating it... No need to go low level.
You can use an UIImageView, just set its opacity to something you like (0.5f for instance) and be sure to set its opaque property to NO.

quartz 2d iphone not drawing

i was trying out quartz 2d today and i got to learn basic drawing and other things, but now, somehow everything i do doesn't draw a thing on the iphone screen! i tried making a new project and starting from scratch but still no luck... first, i made a new view based application, and then created a new file (.h and .m) as a subclass of UIView. In the implementation, i just overrode the drawRect method, and nothing happens! heres the code:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(context, red);
CGContextBeginPath(context);
CGRect rectangle = CGRectMake(0, 0, 400, 200);
CGContextAddRect(context, rectangle);
CGContextSetFillColor(context, red);
CGContextFillPath(context);
}
i have another project that i started before and have the exact same code in and that runs perfectly... so i think i changed some settings or something.. can anyone help?
EDIT: there is nothing i forgot, im sure of it, since i've been doing what i did in my last project, but somehow, it doesnt draw in this one...
I usually forget to set the class of the custom view in Interface Builder (Class popup at the top of the Identity page of the Inspector window). That would explain why drawRect: isn't called.

Take screenshots of a UIView including 3 UITableView(visible+invisible) part

i have a UIView having 3 UITableView and need to take screen shot. But problem is invisible part of the 3 tables . Can anyone help to find a way to take screen shot of the whole view including complete scrolled contents of the tables.
This helps get the contents of a layer (ie. and thus a UIView)
UIGraphicsBeginImageContext(tableView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[tableView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Quartz -- sometimes it starts from a blank view and sometimes it doesn't

I have in front of me two Quartz iPhone apps. In each of them, calls to setNeedsDisplay cause a view to redraw itself. But there is an important difference. In one case (the "Quartz Fun" app from the Mark/Lamarche book "Beginning iPhone development"), the view starts out blank each time. In the other case (the app I am working on), the view starts with whatever was there before, and new graphics are added on top of it.
I can't figure out why this is. Can anyone clue me in?
Thanks.
EDIT #2: I still don't understand what is going on here. But I have figured out that I can clear my view by calling
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.frame);
EDIT #3: showing shortened code:
As a suggested by a commenter, I have edited my app so that the issue occurs with very little code. [The form of the issue is a bit different now, as explained below.]
App delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
DiceView *dv = [[DiceView alloc]initWithFrame: window.frame];
[window addSubview:dv];
[dv release];
[window makeKeyAndVisible];
return YES;
}
DiceView:
- (void)drawRect:(CGRect)rect {
static int nDrawrectCalls = 0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, (nDrawrectCalls%5==0?[UIColor redColor]:[UIColor greenColor]).CGColor);
CGContextMoveToPoint(context, 10, 30+10*nDrawrectCalls);
CGContextAddLineToPoint(context, 300, 30+10*nDrawrectCalls);
CGContextStrokePath(context);
nDrawrectCalls++;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setNeedsDisplay];
}
Everything else is just default stub methods.
Now for the difference. It now appears to start drawing with whatever was on the screen two touches prior. In other words, after touch #2, I see the initial line, plus the line from touch #2 -- but not the line from touch #1. After touch #3, I see the lines from touches #1 and #3. After touch #4, I see the initial line and the lines from touches #2 and #4. And so on.
UIView has a clearsContextBeforeDrawing boolean property that switches between the different behaviors you describe. The default is YES which means the view empties the context before calling drawRect:
Check if that property is set somewhere in the example.
Apparently a view doesn't clear it's context correctly, when no backgroundColor is set on the view. Please set backgroundColor to something other than nil.
I think this is a bug and have filed rdar://8165730.
EDIT
It's not a bug. You have to set the opaque property to NO or the backgroundColor. The behavior is described in the UIView Documentation.
Property clearsContextBeforeDrawing:
The default value of this property is
YES. When set to YES, the current
graphics context buffer in the
drawRect: method is automatically
cleared to transparent black before
drawRect: is invoked. If the view’s
opaque property is also set to YES,
the backgroundColor property of the
view must not be nil or drawing errors
may occur.
If the value of this property is NO,
it is the view’s responsibility to
completely fill its content. Drawing
performance can be improved if this
property is NO—for example, when
scrolling.
Property opaque:
YES if it is opaque; otherwise, NO. If
opaque, the drawing operation assumes
that the view fills its bounds and can
draw more efficiently. The results are
unpredictable if opaque and the view
doesn’t fill its bounds. Set this
property to NO if the view is fully or
partially transparent. The default
value is YES.