-(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;
}
{
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;
}
本文介绍了一种在iOS设备上获取屏幕截图的方法,包括状态栏和主窗口的截图,并通过组合这两种截图来创建最终的全屏截图。代码示例展示了如何使用Objective-C实现这一功能,并保存为PNG文件。
&spm=1001.2101.3001.5002&articleId=51768517&d=1&t=3&u=9be273a5b27c41b1a476285a6b93bda6)
2856

被折叠的 条评论
为什么被折叠?



