【问题标题】:My screen is not reflective (not responding)while taking screenshot in iOS?在 iOS 中截屏时我的屏幕没有反射(没有响应)?
【发布时间】:2016-02-23 09:11:28
【问题描述】:

我有一个应用程序,我在其上实时捕获UIWebView 的屏幕截图并将其发送到服务器。为了开始,我只是更改了视图的背景颜色并截取了它的屏幕截图。

for (int i = 0; i <15; i++)
    {
        [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
        NSLog(@"Delaying");

         self.view.backgroundColor=[self randomColor];


        CGSize size = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height);

        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

        CGRect rec = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);
        [self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

我正在这样做。但是屏幕没有更新,它处于一种冻结模式。它只是在使用最后一种颜色拍摄最后一张屏幕截图后更新。谁能帮我找出我哪里出错了?我需要实时截取我们的应用屏幕截图。

【问题讨论】:

  • 如果你再等一会儿,看门狗只会杀死你的应用程序。 :)

标签: ios uigraphicscontext


【解决方案1】:

[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]; 会导致你的应用冻结,因为它会导致主线程休眠,如果你想延迟截屏,请使用 GCD 的 dispatch_after 函数

for (int i = 0; i <15; i++)
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //body of your forloop goes here
    });
}

如果您不想立即截取初始屏幕截图,可能需要将 i+1 添加到延迟中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 2021-03-10
    • 2019-01-28
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多