iOS截屏耗性能最低的方法(限越狱手机)
2019-06-05 本文已影响0人
rockyMJ
//截屏 过滤掉自定义的window
+(void)createScreenShot:(NSString *)account withPicName:(NSString *)picName WithScale:(CGFloat)scale byFilter:(UIWindow *)window {
//过滤掉小window
[UIView animateWithDuration:0.005 animations:^{
window.alpha=0.0;
} completion:^(BOOL finished) {
[self createScreenShot:account withPicName:picName WithScale:scale WithCompress:0.3];
window.alpha=1.0;
}];
}
//截取小图(自定义路径)
+(void)keepScreenShotWithSmallPicName:(NSString *)name WithImageOfSmallRect:(CGRect)frame fileDir:(NSString *)dir WithScale:(CGFloat)scale
{
UIImage * image=[self getImageWithFullScreenshot:scale];//大图
CGRect rect=frame;
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage * newImage = [UIImage imageWithCGImage:newImageRef];//小图
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
NSData * smallScreenData =UIImagePNGRepresentation(newImage);
//保存起来
[XLHelpClass writeFileContentNsData:[NSString stringWithFormat:@"%@",dir] withName:name withData:smallScreenData];
}
//截取全屏视图
+(UIImage *)getImageWithFullScreenshot:(CGFloat )scale
{
UIView * view = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
CGRect mainRect = view.bounds;
BOOL isLandScape = NO;
if(mainRect.size.width > mainRect.size.height){
isLandScape = YES;
}
UIGraphicsBeginImageContextWithOptions(mainRect.size, NO, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
NSString *sss=[NSString stringWithFormat:@"%@",window];
//NSLog(@"getImageWithFullScreenshot=====%@==",sss);
if([sss rangeOfString:@"FBRootWindow"].location !=NSNotFound){
//NSLog(@"===FBRootWindow===");
/*if(((window.bounds.size.width == mainRect.size.width&&
window.bounds.size.height == mainRect.size.height)
||(window.bounds.size.width == mainRect.size.height&&
window.bounds.size.height == mainRect.size.width))&&(window.hidden==NO)&&([window isKindOfClass:[UIWindow class]])){*/
CGContextSaveGState(context);
if(isLandScape){
//home 键在右
CGContextRotateCTM(context, (CGFloat)(-M_PI_2));
CGContextTranslateCTM(context, -mainRect.size.height, 0);
//home 键在左
//CGContextRotateCTM(context, (CGFloat)(-M_PI_2*3));
//CGContextTranslateCTM(context,0, -mainRect.size.width);
}
if([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];
else
[window.layer renderInContext:UIGraphicsGetCurrentContext()];
CGContextRestoreGState(context);
}
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}