iOS:以编程方式制作屏幕截图的最快,最高效的方法是什么?

时间:2011-11-01 08:42:54

标签: ios performance core-graphics graphicscontext quartz-core

在我的iPad应用程序中,我想制作UIView占据屏幕重要部分的屏幕截图。不幸的是,子视图非常嵌套,因此需要很长时间才能制作屏幕截图,并在之后使页面卷曲。

是否有比“平常”更快的方式?

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如果可能的话,我想避免缓存或重构我的观点。

6 个答案:

答案 0 :(得分:111)

我找到了一种更好的方法,尽可能使用快照API。

我希望它有所帮助。

class func screenshot() -> UIImage {
    var imageSize = CGSize.zero

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

    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
    for window in UIApplication.shared.windows {
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
    }

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

Wanna know more about iOS 7 Snapshots?

Objective-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;
}

答案 1 :(得分:18)


编辑2013年10月3日 更新以支持iOS 7中新的超快速drawViewHierarchyInRect:afterScreenUpdates:方法。


没有。 CALayer的renderInContext:据我所知是唯一的方法。您可以创建这样的UIView类别,以便您自己更容易前进:

的UIView + Screenshot.h

#import <UIKit/UIKit.h>

@interface UIView (Screenshot)

- (UIImage*)imageRepresentation;

@end

的UIView + Screenshot.m

#import <QuartzCore/QuartzCore.h>
#import "UIView+Screenshot.h"

@implementation UIView (Screenshot)

- (UIImage*)imageRepresentation {

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, self.window.screen.scale);

    /* iOS 7 */
    if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])            
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
    else /* iOS 6 */
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage* ret = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return ret;

}

@end

通过这种方式,您可以在视图控制器中说出[self.view.window imageRepresentation],并获得应用的完整屏幕截图。这可能会排除状态栏。

编辑:

我可以补充一下。如果您有一个透明内容的UIView,并且需要带有底层内容的图像表示,您可以获取容器视图的图像表示并裁剪该图像,只需获取子视图的rect并将其转换为容器即可视图坐标系。

[view convertRect:self.bounds toView:containerView]

要裁剪,请参阅此问题的答案:Cropping an UIImage

答案 2 :(得分:10)

iOS 7引入了一种新方法,允许您将视图层次结构绘制到当前图形上下文中。这可以用来非常快地获得UIImage

UIView上实施为类别方法:

- (UIImage *)pb_takeSnapshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);

    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];

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

它比现有的renderInContext:方法快得多。

SWIFT更新:执行相同操作的扩展程序:

extension UIView {

    func pb_takeSnapshot() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale);

        self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)

        // old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())

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

答案 3 :(得分:3)

我将单个功能的答案结合起来,这个功能将针对任何iOS版本运行,即使对于视网膜或非保留设备也是如此。

- (UIImage *)screenShot {
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.view.bounds.size);

    #ifdef __IPHONE_7_0
        #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
            [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
        #endif
    #else
            [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    #endif

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

答案 4 :(得分:2)

对我来说,设置InterpolationQuality还有很长的路要走。

CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);

如果要拍摄非常详细的图像,则可能无法接受此解决方案。如果你正在快照文本,你几乎不会注意到差异。

这样可以显着减少拍摄快照的时间,也可以减少占用内存的图像。

这对于drawViewHierarchyInRect:afterScreenUpdates:方法仍然有用。

答案 5 :(得分:1)

您要求的另一种选择是读取GPU(因为屏幕是从任意数量的半透明视图合成的),这本身也是一种缓慢的操作。

相关问题