-2

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

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

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

-亨利

4

1 回答 1

-1

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

+ (UIImage *)screenshot {

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

    return image;
}

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

于 2013-07-16T21:49:00.127 回答