2

我正在使用以下代码在用户的点击操作上截取应用程序窗口的屏幕截图:

UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, self.view.opaque, 0.0);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];

CGImageRef imageRef = CGImageCreateWithImageInRect([UIGraphicsGetImageFromCurrentImageContext() CGImage], CGRectMake(0, 0, 640, self.view.window.layer.frame.size.height *2));

-执行一些操作imageContext-

myImage = UIGraphicsGetImageFromCurrentImageContext();

现在,屏幕截图很好,但是渲染它所花费的时间令人难以忍受。我需要在用户点击后立即呈现动画,但在屏幕截图图像存储在myImage.

有没有一种快速的方法,就像 Facebook 在墙上做的那样(当打开图像浏览器时),或者他们是否使用了其他一些技术?

4

1 回答 1

1

我设法通过在后台线程和主线程之间分配任务来加速这个过程。

我在后台线程上执行了以下任务dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //code });

  • 开始CGImageContext
  • 创造CGImageRef
  • 使用创建屏幕截图renderInContext
  • 对我需要的 imageRef 执行操作。
  • 将 final 传递UIImage给 a selector,这进一步将图像设置为我想要的变量(UIImageView在我的情况下)。
  • 结束并释放图形上下文。

selector块末尾调用的 in 中,我将图像设置为UIImageViewusingdispatch_async(dispatch_get_main_queue(), ^{ //code
});

于 2013-09-05T06:05:39.003 回答