关于ios:激活iPhone相机操作会使我的应用程序崩溃

Activating iPhone camera action crashes my app

本问题已经有最佳答案,请猛点这里访问。

奇怪的问题:

我的用户通过点击按钮拍照来激活iPhone相机。例如。轻按按钮,相机打开。直到昨天,下面的代码都可以正常工作(并且仍然可以在我自己的手机上运行)。但是,在我们测试组中其他所有人的电话上,只要轻按"相机"按钮,应用程序就会崩溃。知道为什么吗?请参阅下面的代码-我很困惑。注意:每个人都在运行iOS10。

ViewController.h

1
2
3
4
5
6
7
8
9
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate, UINavigationControllerDelegate,  UIImagePickerControllerDelegate> {

}    
    @property (strong, nonatomic) NSMutableArray *photoData;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

    @end

ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (IBAction)takePhoto:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];
}


如果仅在ios10上崩溃,则可能是privacy key setting的问题,这在ios10xcode 8中是必需的。因此您需要在key

下添加

1
  Privacy - Photo Library Usage Description

到您的info.plist中。我认为这可能会解决您的问题。否则,您的代码是完美的。

您可以参考此答案以获取更多键及其说明!