Austin
Austin

Reputation: 51

Screenshot of animated Image

I have one imageview which is changing on every few seconds with curl animation. I want to take the video of that so for that what I am doing is, I take the screenshots every seconds and create video from that. But I am unable to take the screenshot when image is animating, at that time it will return the screenshot in which image is stable or not animating.

I am using below code to take screenshot of image

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

Does any one have idea about it?

Thanks in advance.

-Austin

Upvotes: 2

Views: 218

Answers (2)

ooOlly
ooOlly

Reputation: 2127

According to your code, you seem to snapshot the screen. So just try this:

[[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]

Otherwise:

[yourAnimatedView.layer.presentationLayer snapshotViewAfterScreenUpdates:NO]

Upvotes: 0

Victor Jalencas
Victor Jalencas

Reputation: 1246

I think you should be using:

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

in order to capture the current state of the animation, not the origin or destination states

Upvotes: 1

Related Questions