我在屏幕使用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;
}
确保在截屏或其他昂贵的操作时没有阻塞主线程。