search
LoginSignup
94

More than 5 years have passed since last update.

posted at

updated at

iOS7対応高速(15倍速い)スクリーンキャプチャ

iOS7である場合、UIViewを高速(15倍速い)でキャプチャできます。
iOS6以下は普通のキャプチャになります。

+ (UIImage *)screenCapture:(UIView *)view {
    UIImage *capture;
    UIGraphicsBeginImageContextWithOptions(view.frame.size , NO , 1.0 );

    if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    } else {
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    }

    capture = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return capture;
}

+ (UIImage *)screenCaptureWithView:(UIView *)view rect:(CGRect)rect{
    UIImage *capture;
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
    CALayer *layer = view.layer;
    if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
    } else {
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    capture = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return capture;
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
94