views:

134

answers:

2

Hi All,

I have a bit of code that does what I want and takes a screen shot. Apple have now said that screen shots have to be taken not using UIGetScreenImage(), but UIGraphicsBeginImageContextWithOptions. Could anyone point me in the right direction. Here's a snippet of code that shows how I am doing it at the moment.

CGImageRef inImage = UIGetScreenImage();
UIScreen* mainscr = [UIScreen mainScreen];
CGSize screenSize = mainscr.currentMode.size;
CGImageRef handRef;
if(screenSize.height > 500)
{
  handRef = CGImageCreateWithImageInRect(inImage, CGRectMake(320,460,1,1));
}
else
{
  handRef = CGImageCreateWithImageInRect(inImage, CGRectMake(160,230,1,1));
}
unsigned char rawData[4];
CGContextRef context = CGBitmapContextCreate(
  rawData,
  CGImageGetWidth(handRef),
  CGImageGetHeight(handRef),
  CGImageGetBitsPerComponent(handRef),
  CGImageGetBytesPerRow(handRef),
  CGImageGetColorSpace(handRef),
  kCGImageAlphaPremultipliedLast
);

Anyone how I can do this now?

Thanks to any answers in advance!

+1  A: 

should be something like this:

UIGraphicsBeginImageContext(theView.bounds.size);
theView.layer.renderInContext(UIGraphicsGetCurrentContext());
UIImage* yourFinalScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Where theView is the topmost View you want to render, you could for example use ][UIApplication sharedApplication] keyWindow].

It looks like you are accessing only parts of your screenshot. That could be done more easily with this approach. Just use the view you want to capture, it done not need to be the window.

tonklon
A: 

Thanks. I'll give that a go today. Certainly appears easier than the current approach!

nllk