iOS截屏分享

类似长图截屏分享的功能
把整个view给完整的截下来,做成一张图片。

-(UIImage *)getImageWithFullScreenshot   
{    
    BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");    
        
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;    
        
    CGSize imageSize = CGSizeZero;    
    if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation)    
        imageSize = [UIScreen mainScreen].bounds.size;    
    else    
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);    
        
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);    
    CGContextRef context = UIGraphicsGetCurrentContext();    
        
    for (UIWindow *window in [[UIApplication sharedApplication] windows])    
    {    
        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);    
            
        // Correct for the screen orientation    
        if(!ignoreOrientation)    
        {    
            if(orientation == UIInterfaceOrientationLandscapeLeft)    
            {    
                CGContextRotateCTM(context, (CGFloat)M_PI_2);    
                CGContextTranslateCTM(context, 0, -imageSize.width);    
            }    
            else if(orientation == UIInterfaceOrientationLandscapeRight)    
            {    
                CGContextRotateCTM(context, (CGFloat)-M_PI_2);    
                CGContextTranslateCTM(context, -imageSize.height, 0);    
            }    
            else if(orientation == UIInterfaceOrientationPortraitUpsideDown)    
            {    
                CGContextRotateCTM(context, (CGFloat)M_PI);    
                CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);    
            }    
        }    
            
        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;    
}  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,687评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 望着坐卧在茶几前的地毯上,把书包整理的妹妹,我的心头实在不是一番滋味。没想到时间究竟还是这样不经意间就逝去了一大截...
    okay_先生阅读 283评论 0 3
  • 这几天每天晚上10点多开始下雾,持续到早上日出,前天返京奔忙整日,返家已经是半夜一点,疲累之极,昨天就没外出,除了...
    尘世知行者阅读 192评论 0 0
  • 每天都是特斯拉的新闻,挤爆了头条,我有时候会怀疑:资本绑架着媒体,不停地进行造神运动。苹果,皮克斯,next之于乔...
    乔杜先生阅读 691评论 0 0