Reputation: 4728
I have a little bit of code I got off the internet for grabbing a screen capture to the camera roll, it works great, here it is:
CGRect screenRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);
UIGraphicsEndImageContext();
everything working perfect for iPhone, but now im changing things up for the iPad, and i need to capture a different rectangle than the whole screen, so I'm specifying a different rectangle like this:
CGRect screenRect;
switch (runningOniPad) {
case 0: // running on iPhone..
screenRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);
break;
default: // yes, running on iPad..
screenRect = CGRectMake(56, 478, 662, 262);
break;
}
well the rectangle I'm getting in the camera roll is of the SIZE im specifying, but its origin appears to be (0,0)... perhaps its the [self.view.layer renderInContext:ctx];??
if anyone can help I would surely appreciate it, thank you so much :)
Upvotes: 1
Views: 1451
Reputation: 12582
Jeff, just an explanation of what is going on there:
"ctx" makes a little "offscreen working area".
OK?
In fact, "ctx" lets you refer to that notepad or sketchblock.
Now you ask, how big is that sketchblock? In fact, the original code writer made that sketchblock the same size as the current screen.
Note that the variable which is called "screenRect" is badly named. It should be called howBigWillOurSketchBlockBe.
Makes sense so far?
So, firstly go through the code and change screenRect to howBigWillOurSketchBlockBe. All done.
Now -- as you can now see, all you are doing with your new code, is changing the size of the sketchblock.
Makes sense?
Now, you see the line that says "renderInContext" ... that takes the first thing mentioned, to wit self.view.layer, and renders it in to your sketchblock.
Of course, it starts using the bottom left of self.view.layer.
Since your sketchblock is now quite small (look at your variable howBigWillOurSketchBlockBe) and you will see what is happening!
So the important thing to understand is that "howBigWillOurSketchBlockBe" (was previously poorly named "screenRect") is simply and only the SIZE of the SKETCHBLOCK which you have set up to doodle on.
So now you're wondering how to solve the problem! I think you will need to look down to the rooster on this page,
Does applying a little transform help? CGContextTranslateCTM ...
Hope it helps!
Upvotes: 2