访问相机-单击取消按钮时,延迟关闭uiactionview



当我们尝试访问相机时,我们会得到一个UIActionView,它会告诉你是要访问相机、相册还是取消。

我的代码运行得很好,但当我点击取消按钮时,大约需要30秒才能取消。我还没有在cancel方法中编写任何代码。我只是把它空着。

为什么会延迟?我该如何预防?

代码

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker setDelegate:self];
    if (buttonIndex == 0) {

        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [self.navigationController presentModalViewController:imagePicker animated:YES]; 
    } else if (buttonIndex == 1) {

        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self.navigationController presentModalViewController:imagePicker animated:YES];
    } else {

    } 
}

如果您正在运行连接到Xcode的此代码(即它正在调试器中运行),则在它尝试分配UIImagePickerController时,您将获得暂停。这在调试器中非常缓慢。试着在不插电的情况下运行,它应该会更快,也试着不要初始化它,除非你需要它。

重复代码可能看起来很糟糕,但你可能可以通过以下方式来帮助它:

imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];

在if语句中。

希望这有帮助:)

试试这个:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        imagePicker = [[UIImagePickerController alloc] init];
        [imagePicker setDelegate:self];
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [self.navigationController presentModalViewController:imagePicker animated:YES]; 
    } else if (buttonIndex == 1) {
        imagePicker = [[UIImagePickerController alloc] init];
        [imagePicker setDelegate:self];
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self.navigationController presentModalViewController:imagePicker animated:YES];
    } else {
        // Cancel button code here
    }
}

这与的方式略有不同

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex != actionSheet.cancelButtonIndex) {
    imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker setDelegate:self];
    if (buttonIndex == 0) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [self.navigationController presentModalViewController:imagePicker animated:YES]; 
    } else if (buttonIndex == 1) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self.navigationController presentModalViewController:imagePicker animated:YES];
    }
} else {
    // Cancel button code goes here
}

}

因此,我们首先检查它是否不是取消按钮,如果不是,我们创建UIImagePickerController,然后在需要它的两种情况下使用它。如果它是取消按钮,那么它什么也不做。

这看起来应该更快乐一些。

我遇到了一个听起来像是这个问题的问题。我通过改变ActionSheet的呈现方式来解决这个问题。

我改变了这个:

[actionSheetMedia showInView:self.view];

到此:

[actionSheetMedia showInView:self.tabBarController.tabBar];

现在,滞后和无响应的取消按钮不再存在。

最新更新