本文主要是介绍IOS 截屏||截图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#pragma mark - 截屏方法/**
*@return UIImage
*/
+(UIImage *)ScreenShots
{
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
}
else
{
UIGraphicsBeginImageContext(imageSize);
}
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *image = nil;
for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, [window center].x, [window center].y);
CGContextConcatCTM(context, [window transform]);
CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
} else {
[window.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
}
CGContextRestoreGState(context);
}
}
UIGraphicsEndImageContext();
DLog(@"Suceeded!");
return image;
}
//截图 ,将下面的window改成你要截图的对象即可
-(UIImage *)captureAndSaveView:(UIView *)view{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 1.0); //NO,YES 控制是否透明
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = nil;
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
} else {
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
return image;
}
这篇关于IOS 截屏||截图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!