【问题标题】:Capture Screen while animating在制作动画时捕获屏幕
【发布时间】:2013-12-27 16:16:00
【问题描述】:

我需要在动画制作时捕捉视图。以下代码我用来在动画时捕获图像。

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

它只捕获结果图像

【问题讨论】:

    标签: ios animation calayer screen-capture


    【解决方案1】:

    您可以使用具有动画对象的视图副本,并使用该副本视图来捕获屏幕。即

    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);
    

    【讨论】:

    • 它因错误而崩溃--[UIView mutableCopyWithZone:]:unrecognized selector sent to instance
    • 用 NSData *tempArchiveView = [NSKeyedArchiver archivedDataWithRootObject:self.view] 替换代码的第一行之后; UIView *copyView = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView];空白屏幕保存在照片库中
    • UIView 没有实现复制方法。
    • 这个问题有什么解决办法吗?
    • 嘿,你可以使用这行代码来处理一个 UIView 对象 UIView *copyView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.view]];
    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2012-05-16
    相关资源
    最近更新 更多