iOS|iOS - 截取屏幕某一区域
方法一:(截图较清晰)
-(UIImage*) imageByCaptureScreen{
if(&UIGraphicsBeginImageContextWithOptions!=NULL)
{
//UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size,NO,0);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(kScreenWidth, kScreenHeight - 64 - 50),NO,0);
}
else
{
UIGraphicsBeginImageContext(CGSizeMake(kScreenWidth, kScreenHeight - 64 - 50));
}
CGContextRef context = UIGraphicsGetCurrentContext();
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]);
//if(IOS7_OR_LATER)
//{
//CGContextTranslateCTM(context,
//-[window bounds].size.width* [[window layer]anchorPoint].x,
//-[window bounds].size.height* [[window layer]anchorPoint].y);
//}
//else
//{
CGContextTranslateCTM(context,
-[window bounds].size.width* [[window layer]anchorPoint].x,
-([window bounds].size.height)* [[window layer]anchorPoint].y-64);
//}
[[window layer]renderInContext:context];
CGContextRestoreGState(context);
}}UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
【iOS|iOS - 截取屏幕某一区域】方法二:(截取的图片清晰度不如方法一)
-(UIImage *)fullScreenshots{UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContext(screenWindow.frame.size);
//全屏截图,包括window[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect1 =CGRectMake(0 , 64 , kScreenWidth , kScreenHeight - 64 - 50);
UIImage * imgeee = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([viewImage CGImage], rect1)];
return imgeee;
}
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 屏幕另一边的纠结
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密
- iOS,打Framework静态库