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;
    
}


方法二:(截取的图片清晰度不如方法一)

-(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;
    
}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,860评论 25 709
  • 公司面临困境,资金周转不灵,业务量萎缩……作为员工深知其发展前景,若要放弃,另觅栖处,可谓难以开口。 怎么能在公司...
    pp彭阅读 394评论 5 0
  • 连做梦 都该是五彩斑斓的年纪 我闭上眼 看见自己的未来 ...
    奇思妙想小辣鸡阅读 132评论 0 0
  • 小学阶段学习奥数应该掌握哪些知识点呢?下面是小学奥数题必须掌握的30个知识点之一的“二进制及其应用”。 小学奥数二...
    沪江中小幼阅读 1,001评论 0 0
  • 在生活中,总有那么一些孩子,活泼可爱而又聪明懂事,让人爱不释手,他们就是“别人家的孩子”,但也有那么一些捣蛋...
    零下39度阅读 296评论 1 2