77

iPadアプリで、画面の大部分を占めるU​​IViewのスクリーンショットを作成したいと思います。残念ながら、サブビューはかなり深くネストされているため、スクリーンショットを作成してページをアニメーション化するのに時間がかかります。

「通常の」方法よりも速い方法はありますか?

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

可能であれば、ビューのキャッシュや再構築は避けたいと思います。

4

6 に答える 6

112

可能な限りスナップショット 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!
}

iOS 7 のスナップショットについて詳しく知りたいですか?

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;
}
于 2011-11-05T00:41:34.427 に答える
18

編集 2013 年 10 月 3 日 iOS 7 の新しい超高速 drawViewHierarchyInRect:afterScreenUpdates: メソッドをサポートするように更新されました。


いいえ、CALayer の renderInContext: は、私が知る限り、これを行う唯一の方法です。このような UIView カテゴリを作成して、今後の作業を容易にすることができます。

UIView+スクリーンショット.h

#import <UIKit/UIKit.h>

@interface UIView (Screenshot)

- (UIImage*)imageRepresentation;

@end

UIView+スクリーンショット.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]、View Controller で発言して、アプリの完全なスクリーンショットを取得できる場合があります。ただし、これによりステータスバーが除外される場合があります。

編集:

と、付け加えさせてください。透明なコンテンツを含む UIView があり、下にあるコンテンツを含む画像表現も必要な場合は、サブビューの rect を取得してコンテナーに変換するだけで、コンテナー ビューの画像表現を取得してその画像をトリミングできます。ビューの座標系。

[view convertRect:self.bounds toView:containerView]

トリミングするには、この質問への回答を参照してください: UIImage のトリミング

于 2011-11-01T10:25:30.870 に答える
2

私にとって、InterpolationQuality の設定は大いに役立ちました。

CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);

非常に詳細な画像のスナップショットを作成している場合、このソリューションは受け入れられない場合があります。テキストのスナップショットを作成している場合、違いはほとんどわかりません。

これにより、スナップショットを撮る時間が大幅に短縮され、メモリ消費量が大幅に削減されました。

これは、drawViewHierarchyInRect:afterScreenUpdates: メソッドを使用する場合でも有益です。

于 2013-10-11T21:26:14.363 に答える
1

代替手段として求めているのは、GPU を読み取ることです (画面は任意の数の半透明ビューから合成されるため)、これも本質的に遅い操作です。

于 2011-11-07T23:34:53.260 に答える