美文网首页
iOS 截屏代码

iOS 截屏代码

作者: penggy | 来源:发表于2016-09-13 14:14 被阅读0次

Object-C

+ (UIImage *)screenshot
{
    CGSize imageSize = CGSizeZero;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = [UIScreen mainScreen].bounds.size;
    } else {
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    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);
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        } else {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

swift

class func screenshot() -> UIImage {
    var imageSize = CGSizeZero

    let orientation = UIApplication.sharedApplication().statusBarOrientation
    if UIInterfaceOrientationIsPortrait(orientation) {
        imageSize = UIScreen.mainScreen().bounds.size
    } else {
        imageSize = CGSize(width: UIScreen.mainScreen().bounds.size.height, height: UIScreen.mainScreen().bounds.size.width)
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
    let context = UIGraphicsGetCurrentContext()
    for 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)
        if orientation == .LandscapeLeft {
            CGContextRotateCTM(context, CGFloat(M_PI_2))
            CGContextTranslateCTM(context, 0, -imageSize.width)
        } else if orientation == .LandscapeRight {
            CGContextRotateCTM(context, -CGFloat(M_PI_2))
            CGContextTranslateCTM(context, -imageSize.height, 0)
        } else if orientation == .PortraitUpsideDown {
            CGContextRotateCTM(context, CGFloat(M_PI))
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height)
        }
        if window.respondsToSelector("drawViewHierarchyInRect:afterScreenUpdates:") {
            window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
        } else if let context = context {
            window.layer.renderInContext(context)
        }
        CGContextRestoreGState(context)
    }

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

相关文章

  • flutter:截屏

    1.flutter-截屏组件 2.flutter-截屏插件 3.flutter-iOS原生截屏 iOS代码 4.获...

  • iOS屏幕截图功能

    iOS7.0之前的系统,可以通过以下代码实现截屏功能。 iOS7.0之后,系统中封装了截屏的方法- (UIView...

  • iOS 截屏代码

    Object-C swift

  • 针对WKWebView进行内容的截屏。

    最近项目中需要截屏分享的功能,之前也了解过iOS截屏的技术,并写了关于截屏的笔记,兴冲冲的拿着代码稍作修改,对WK...

  • (最新)iOS截屏

    ios webview 截屏:ios截屏 前言:介绍一下截屏有很多种做法1:截当前屏幕内容2:截整个视图的所有内容...

  • ios截屏

    ios截屏

  • iOS 系统自带截屏分享

    分享一篇iOS系统自带截屏分享 使用方案 简单粗暴,贴上代码

  • iOS 应用内截屏分享

    需求:捕获用户截屏操作,并建议用户截屏后的操作。虽然iOS11 有系统的截屏,但 APP 内截屏可便捷操作。 封装...

  • 截屏代码

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectG...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

网友评论

      本文标题:iOS 截屏代码

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