iOS-截取屏幕某一区域

作者: Joh蜗牛 | 来源:发表于2017-11-08 10:13 被阅读34次

    方法一:(截图较清晰)

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

    相关文章

      网友评论

        本文标题:iOS-截取屏幕某一区域

        本文链接:https://www.haomeiwen.com/subject/eydemxtx.html