【问题标题】:Getting errors when calling UIGetScreenImage() in background在后台调用 UIGetScreenImage() 时出错
【发布时间】:2012-10-18 07:03:56
【问题描述】:

当我的应用进入后台时,出现如下错误信息:

012-10-18 14:38:05.964 SoundRecorder[693:14603] *** Terminating app due to uncaught
exception 'NSGenericException', reason: 'Cannot call UICreateScreenImage() while app
is in the background.'

这是我的代码:

- (void) getImage
{
    CGImageRef screen = UIGetScreenImage();
    UIImage* image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    NSLog(@"1");
    [NSThread sleepForTimeInterval:0.5];
}

- (void) applicationDidEnterBackground: (UIApplication*) application
{
    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{  
        [app endBackgroundTask:bgTask];  
        bgTask = UIBackgroundTaskInvalid;  
    }];  
    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
        while(1){
            [self getImage];
            if(breakpoints != 1)
                break;
        }
        NSLog(@"run at backgroud!");
        [app endBackgroundTask:bgTask];  
        bgTask = UIBackgroundTaskInvalid;  
     }); 
}

【问题讨论】:

  • [self getImage] 里面有什么?
  • CGImageRef UIGetScreenImage(void); -(void)getIamge{ CGImageRef screen = UIGetScreenImage(); UIImage* 图像 = [UIImage imageWithCGImage:screen]; CGImageRelease(屏幕); UIImageWriteToSavedPhotosAlbum(图像,自我,无,无); NSLog(@"1"); [NSThread sleepForTimeInterval:0.5]; }
  • 最好把代码加到问题里,在cmets里很难看懂。我已经为您编辑了问题并添加了缺少的代码,您现在可以删除评论。

标签: iphone


【解决方案1】:

嗯,根据错误信息,当您的应用在后台时,无法调用UIGetScreenImage() 和相关函数。这是有道理的,这将是一个巨大的安全漏洞。

【讨论】:

    【解决方案2】:

    你不应该使用 dispatch_async,你必须在后台线程中调用这个函数。

    在主线程/UI 线程中调用 dispatch_async 可能会导致函数被多次调用(因为 async 立即返回),甚至在最后一个完成之前就会使队列不堪重负。

    如果您在 UI 以外的其他线程中调用它,我可以确认它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 2013-02-04
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 2015-03-16
      相关资源
      最近更新 更多