动画图片的屏幕截图

时间:2015-02-05 05:55:20

标签: ios iphone cgcontext objective-c-2.0 uigraphicscontext

我有一个使用curl动画每隔几秒就会改变的imageview。我想拍摄视频,因为我正在做的是,我每隔几秒拍摄一次屏幕截图并从中创建视频。 但是当图像是动画时我无法截取屏幕截图,那时它将返回图像稳定或不动画的屏幕截图。

我使用下面的代码来截取图片

{
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[[window layer] presentationLayer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

有人对此有所了解吗?

提前致谢。

-Austin

2 个答案:

答案 0 :(得分:1)

我认为你应该使用:

[[[[window contentView] layer] presentationLayer] renderInContext:context];

以捕获动画的当前状态,而不是原始状态或目标状态

答案 1 :(得分:0)

根据您的代码,您似乎快照屏幕。 所以试试这个:

[[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]

否则:

[yourAnimatedView.layer.presentationLayer snapshotViewAfterScreenUpdates:NO]