【问题标题】:Screenshot of animated Image动画图像的屏幕截图
【发布时间】:2015-02-05 05:55:19
【问题描述】:

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

我正在使用下面的代码来截取图像

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

有人知道吗?

提前致谢。

-奥斯汀

【问题讨论】:

  • 尝试缩短屏幕截图之间的时间间隔
  • 你尝试的时间间隔是多少?
  • 我已经尝试过从 1.0 到 1/30 秒的值...
  • 愿意接受答案吗?

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


【解决方案1】:

我认为你应该使用:

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

为了捕捉动画的当前状态,而不是起点或终点状态

【讨论】:

    【解决方案2】:

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

    [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]
    

    否则:

    [yourAnimatedView.layer.presentationLayer snapshotViewAfterScreenUpdates:NO]
    

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多