我打开第二次后,UIImagePickerController没有拍照和冻结

时间:2015-01-22 19:44:45

标签: ios objective-c uiimagepickercontroller

我有简单的单例类,负责创建和呈现UIImagePickerController。几个小时前它工作得很好。我所做的就是将代码从ViewController移到单独的单例类(post的末尾相关代码)。当我试图撤消所有更改以恢复到之前的状态时,问题仍然存在。

每当我展示相机时,我都会收到警告(不确定是否相关) "快照未渲染的视图会导致空快照。确保您的视图在屏幕更新后的快照或快照之前至少呈现一次。"

然后,当我按下快门按钮时,没有任何反应,没有声音,没有焦点,没有拍照,也没有回调到我的应用程序。 当我点击取消时,我得到回调并解除控制器没有问题。

现在当我再次打开相机时,屏幕几乎冻结,我得到了控件和所有内容,但屏幕没有更新(实际上它确实如此,但每分钟只有一次或两次)

@interface AddPhotoManager()

@property (nonatomic, weak) UIViewController *controller;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;

@end

@implementation AddPhotoManager

+ (instancetype)sharedInstance
{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (void)addNewPhotoFromController:(UIViewController*)controller
{
    self.controller = controller;

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Photo library", nil];
    [sheet showInView:self.controller.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0 || buttonIndex == 1) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            // Will get here on both iOS 7 & 8 even though camera permissions weren't required
            // until iOS 8. So for iOS 7 permission will always be granted.
            if (granted) {
                // Permission has been granted. Use dispatch_async for any UI updating
                // code because this block may be executed in a thread.
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self presentPickerController:buttonIndex == 0];
                });
            } else {
                // Permission has been denied.
            }
        }];
    }
}

- (void)presentPickerController:(bool)camera
{
    self.imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController.sourceType = camera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    self.imagePickerController.delegate = self;
    [self.controller presentViewController:self.imagePickerController animated:false completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:true completion:nil];

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

    // TODO: upload the image

    self.imagePickerController = nil;
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:true completion:nil];

    self.imagePickerController = nil;
}

单身人士只需拨打

即可使用
[[AddPhotoManager sharedInstance] addNewPhotoFromController:self];

来自任何视图控制器

编辑: 将imagePickerController存储在强大的属性中似乎没有必要,但我不确定所以我试过了。但是,两种情况下的结果相同。

编辑2: 我在新的测试应用程序中尝试了完全相同的代码,所有工作都没有问题,所以它必须干扰应用程序的其他部分?任何想法可能是什么?我尝试删除应用程序以重置权限,甚至更改了包ID,但没有任何帮助

0 个答案:

没有答案