在我的iPad应用程序中,我想制作一张UIView占据屏幕很大一部分的屏幕截图.不幸的是,子视图嵌套得很深,所以需要很长时间才能制作屏幕截图并在之后设置页面curl 的动画.

有没有比"通常"更快的方法?

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

如果可能,我希望避免缓存或重新构建我的视图.

推荐答案

我已经找到了一种更好的方法,只要有可能就使用快照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;
}

Ios相关问答推荐

设置托管在DigitalOcean上的iOS通用链接

如何在KMM项目中处理PHPickerViewController的回调?

如果使用 .onAppear 设置,为什么 SwiftUI Picker 不显示正确的选定值

React Native IOS base64 编码图像不显示

NSOperation 有延迟 - 它是异步的还是同步的?

卡在 Apple 的 SwiftUI 教程第 5 章(更新应用数据)

渲染的海边重定向在物理 iOS 设备上不起作用

如何在Collection 后更改并保留按钮 colored颜色 ,然后在 SwiftUI 中切换回来?

UIViewRepresentable - MKMapView 没有得到更新

clipShape swift的三元

UIImageView 使用 CGAffineTransform 错误旋转

如何在不同设备的segment控制中管理文本大小(text size)?

UILabel - 字符串作为文本和链接

UIView 动画与 CALayers

如何将自己项目中的类导入 Playground

如何在 UILabel 中找到文本子字符串的 CGRect?

如果我的分发证书过期会怎样?

Flutter:如何创建一个新项目

Xcode 6 进程启动失败:try 启动应用程序超时

dismissModalViewController 并传回数据