1. 程式人生 > >iOS 截圖(包含statusbar)

iOS 截圖(包含statusbar)

-(id)ScreenShot
{
UIImage * image[2];
UIView * view;
UIApplication * app = [UIApplication sharedApplication];
for(int i =0 ;i<2;i++)
{
if(i==0)
{
view = [app valueForKey:@"_statusBar"];
}
else
{
view =[app keyWindow];
}
UIGraphicsBeginImageContext(view.frame.size);//全屏截圖,包括window
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    image[i] = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
UIGraphicsBeginImageContext(image[1].size);
[image[1] drawInRect:CGRectMake(0,0, image[1].size.width, image[1].size.height)];
[image[0] drawInRect:CGRectMake(0,0, image[0].size.width, image[0].size.height)];
UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"myScreenShot2.png"]];
    [UIImagePNGRepresentation(resultingImage) writeToFile:filePath atomically:YES];
return filePath;


}