iOS - 使用状态栏截取屏幕截图

时间:2013-07-16 20:02:35

标签: ios objective-c uiimage screenshot statusbar

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

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

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

-Henry

1 个答案:

答案 0 :(得分:-1)

这是我最终做到这一点的方式......

+ (UIImage *)screenshot {

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

    return image;
}

虽然App Store不是可以批准的,但这是最好的方法。