【问题标题】:iOS - Take screenshot with status bar [duplicate]iOS - 使用状态栏截屏 [重复]
【发布时间】:2013-07-16 20:02:35
【问题描述】:

我从苹果文档中获得了这段代码...

+ (UIImage *)screenshot {
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    } else {
        UIGraphicsBeginImageContext(imageSize);
    }

    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]);

            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);


            [[window layer] renderInContext:context];


            CGContextRestoreGState(context);
        }
    }


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return image;
}

但是,我有一个小问题。我想在我的屏幕截图中包含状态栏。我该怎么做呢?还是有更好的方法来做到这一点?

-亨利

【问题讨论】:

  • @livingtech 哎呀。对不起。让我看看。

标签: ios objective-c uiimage screenshot statusbar


【解决方案1】:

这就是我最终这样做的方式......

+ (UIImage *)screenshot {

    CGImageRef screen = UIGetScreenImage();
    UIImage *image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);

    return image;
}

虽然它不是 App Store 认可的,但它是最好的方法。

【讨论】:

  • 这个方法很神奇,但是UIGetScreenImage是私有的api,App Store不允许吗?又怎么用它来截取指定区域的截图呢?
猜你喜欢
  • 2016-03-06
  • 1970-01-01
  • 2013-09-20
  • 2011-05-28
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 2014-05-13
相关资源
最近更新 更多