user3085992
user3085992

Reputation: 163

Capture Screen while animating

I need to capture view while it is animating. Following code i am using to capture image while animation.

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.10 target: self selector:@selector(captureImage:) userInfo: nil repeats: YES];

[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];

UIImage * toImage = [[AppDelegateObj resultedImageArray]objectAtIndex:1];

[UIView transitionWithView:beforeImgView 
duration:5 
options:UIViewAnimationOptionTransitionCrossDissolve 
animations:^{ 
beforeImgView.image = toImage;
} 
completion:^(BOOL finished) {
[myTimer invalidate];
}];
}

-(void)captureImage:(NSTimer*) t {
    CALayer *layer;
    layer = [self.view.layer presentationLayer];
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextClipToRect (UIGraphicsGetCurrentContext(),self.view.frame);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

It only captures resultant image

Upvotes: 2

Views: 444

Answers (1)

Anuj
Anuj

Reputation: 830

You can use the copy of the view which has animating objects and use that copy view for capturing the screen. i.e.

UIView *copyView = [self.view mutableCopy];

CALayer *layer;
layer = [copyView.layer presentationLayer];
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),copyView.frame);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

Upvotes: 1

Related Questions