我正在尝试UIView
使用下面的方法截取我的屏幕快照。仅当我不尝试在“后台”中执行此操作时,它才有效。但是我需要在后台使用它,因为每次启动时,我都会承担可怕的ui责任-几秒钟内什么都不会起作用。
这种方法有效,但是mach_msg_trap
当我暂停它时,它会一直停留在消息中。
-(void)takeShoot{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
float height;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
height = 600;
}else{
height = 500;
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), YES, 0.6);
[self.view drawViewHierarchyInRect:CGRectMake(0, -60, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:YES];
self.sharedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
}
当我被解雇时,NSOperation
情况会变得更糟-它会无缘无故地做到这一点。第三种方法performSelectorInBackground
只是垃圾,如果每次uiwebview
加载完成时都触发该方法,那么将有数十亿个线程,那我认为一点都不好。
因此,我该如何在后台拍摄屏幕快照(不在上mainThread
),而不杀死或冻结任何人?
您必须在主线程上截屏。这是UI稳定的唯一线程。
如果您有几秒钟的问题,我怀疑您还有其他问题,这只是暴露而已。最有可能是您在后台线程上进行的其他UIKit调用。不支持该功能,并且会导致您描述的症状。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句