NSTimer 在截取 UiView iOS 的屏幕截图时停止?



我在屏幕使用NStimer方法更新屏幕时拍摄UIView的屏幕截图,但在截取视图屏幕截图时Timer停止

我经常截取 5 到 8 张屏幕截图以捕获视图上的更新

但是计时器停止并且无法根据需要获得确切的屏幕截图

我的代码:

-(void)StartAnmation {
timer= [NSTimer scheduledTimerWithTimeInterval:0.2 target:self  selector:@selector(methodToAddImages) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)methodsave
{
for (int i=0; i<5; i++) {
UIImage *img=[self screenshotimageWithView:self.mainview];
[gifimages addObject:img];
}
}

截图

- (UIImage ) screenshotimageWithView:(UIView )view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
NSData *miths=UIImagePNGRepresentation(img);
UIImage *imge=[UIImage imageWithData:miths];
UIGraphicsEndImageContext();
return imge;
}

确保在截屏或其他昂贵的操作时没有阻塞主线程。

最新更新