我正在尝试定期捕获 iDevice 的屏幕(每秒或当用户触摸屏幕时)。我正在使用子类 UIView 执行此操作,其中 hitTest 方法调用以下内容:
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *pngTitle = [NSString stringWithFormat:@"Documents/Test%d.jpg", imageIdentifier];
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:pngTitle];
int tempIdentifier = imageIdentifier+1;
imageIdentifier = tempIdentifier;
[UIImageJPEGRepresentation(capturedScreen, 0.4f) writeToFile:pngPath atomically:YES];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
捕获屏幕内容可以正常工作,但是,它也会导致性能大幅下降,以至于我嵌入它的应用程序几乎无法使用。例如,滑动手势变得如此缓慢,不再在滚动视图上注册。
有没有办法捕捉不影响性能的屏幕图像(或至少不影响这么多)?
谢谢!