1

我正在尝试定期捕获 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];

捕获屏幕内容可以正常工作,但是,它也会导致性能大幅下降,以至于我嵌入它的应用程序几乎无法使用。例如,滑动手势变得如此缓慢,不再在滚动视图上注册。

有没有办法捕捉不影响性能的屏幕图像(或至少不影响这么多)?

谢谢!

4

1 回答 1

2

用于您的要求:我假设您每次都GCD打电话:getScreen1 second

-(void)getScreen
{
 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
 dispatch_async(queue, ^{
    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];
    imageIdentifier = imageIdentifier+1;
    [UIImageJPEGRepresentation(capturedScreen, 0.4f) writeToFile:pngPath atomically:YES];

    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];

  dispatch_sync(dispatch_get_main_queue(), ^{
    [self performSelector:@selector(getScreen) withObject:nil afterDelay:1.0];
  });
  });
 }
于 2013-04-30T09:35:29.910 回答