2010-07-23 166 views
1

我有一些代码可以完成我想要的功能并拍摄屏幕截图。现在苹果已经表示屏幕截图不得不采用UIGetScreenImage(),而是UIGraphicsBeginImageContextWithOptions。任何人都可以指出我正确的方向。下面是一段代码,显示了我现在正在做的事情。iPhone上的屏幕截图

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 
); 

无论如何我现在可以做到这一点?

感谢任何答案提前!

回答

2

应该是这样的:

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

哪里theView是要呈现最上面的视图,例如,你可以使用] [UIApplication的sharedApplication] keyWindow。

看起来您只能访问部分屏幕截图。这种方法可以更容易地完成。只要使用你想要捕捉的视图,它不需要成为窗口。