Reputation: 77
A UIView
contains an animating image (UIImage
).
I want to take a screenshot of the parent view in a loop using renderInContext:UIGraphicsGetCurrentContext
While capturing screenshot of the parent view (previewContainer
), it doesn't capture image as animated.
Only the last position of the image in the view is captured every time (will display in thmbView
).
code to capture
- (void) startCapturing{
UIGraphicsBeginImageContext(previewContainer.frame.size);
[previewContainer.layer renderInContext:UIGraphicsGetCurrentContext()];
thmb.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
double delayInSeconds = 0.2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (sec++ < 15) {
[self startCapturing];
}
});}
code to animate
- (void) animate{
CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
[appearance setValue:@"animation1" forKey:@"id"];
appearance.delegate = self;
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
appearance.duration = 10;
appearance.fromValue = [NSNumber numberWithFloat:0];
appearance.toValue = [NSNumber numberWithFloat:340];
appearance.repeatCount = 1;
appearance.fillMode = kCAFillModeForwards;
appearance.removedOnCompletion = NO;
[imgPreview.layer addAnimation:appearance forKey:@"transform.translation.y"];
}
Upvotes: 1
Views: 1618
Reputation: 496
Only way to take screenshots of animation in the view that you can render the presentation layer.
Instead of this line
[previewContainer.layer renderInContext:UIGraphicsGetCurrentContext()];
Use this
[previewContainer.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
Upvotes: 4
Reputation: 196
Use the screenshot method and try it, no matter view or animation action your can get UIImage object.
- (UIImage *)screenshotImageOfItem:(UIView *)item
{
UIGraphicsBeginImageContextWithOptions(item.bounds.size, item.isOpaque, 0.0f);
[item.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Upvotes: 2