UIImagePickerController:忽略拍照请求;相机尚未准备好。

5

我在我的应用程序中使用相机。

我希望能够在3秒钟后自动拍照。代码完全可以运行,但有一种情况除外...

如果在相机启动的期间,应用程序进入后台,例如来电或用户按下主页键等方式,则当应用程序恢复时,相机不会继续启动,并且在控制台上会出现此警告:

UIImagePickerController:忽略拍照请求;相机尚未准备好。

当出现这种情况时,我想重新启动相机,该怎么办?

2个回答

0

我们可以使用这些应用程序委托函数

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

0

我为解决这个问题所做的事情:

- (void)viewDidAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(cameraIsReady:)
                                                 name:AVCaptureSessionDidStartRunningNotification object:nil];

    self.ready = NO;
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        // There is not a camera on this device, so don't show the camera button.
        [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    } else {
        [self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
    }
}

- (void)cameraIsReady:(NSNotification *)notification {
    self.ready = YES;
}

- (IBAction)takePhoto:(id)sender {
    if (self.ready) {
        [self.imagePickerController takePicture];
    }
    self.ready = NO;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接