如何在为该视图内的图像设置动画时捕获UIView快照

时间:2014-08-26 17:02:42

标签: ios objective-c

UIView包含动画图片(UIImage)。

我想使用renderInContext:UIGraphicsGetCurrentContext

在循环中截取父视图的屏幕截图

在捕获父视图(previewContainer)的屏幕截图时,它不会将图像捕获为动画。

每次只捕获视图中图像的最后位置(将显示在thmbView中)。

捕获代码

- (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];
    }
});}

设置动画的代码

- (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"];

}

2 个答案:

答案 0 :(得分:2)

使用截图方法并尝试它,无论是视图还是动画操作都可以获得UIImage对象。

- (UIImage *)screenshotImageOfItem:(UIView *)item 
{

UIGraphicsBeginImageContextWithOptions(item.bounds.size, item.isOpaque, 0.0f);

[item.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

答案 1 :(得分:2)

只能在视图中截取动画截图,以便渲染表示层。

而不是这一行

[previewContainer.layer renderInContext:UIGraphicsGetCurrentContext()];

使用此

[previewContainer.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];