运行UIImagePickerController时模拟器崩溃Xcode 7

时间:2015-09-09 08:55:13

标签: ios objective-c uiimagepickercontroller

当我尝试打开UIImagePickerController

时,我的iPhone 5模拟器崩溃了

我的.m代码:

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a    nib.

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertView *myAlertView = [[UIAlertView alloc]  initWithTitle:@"Error"
                                                            message:@"Device has no camera"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles: nil];

    [myAlertView show];

}

}

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

}

- (IBAction)takePhoto:(UIButton *)sender {

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

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

}

 - (IBAction)selectPhoto:(UIButton *)sender {

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

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


}

 #pragma mark - Image Picker Controller delegate methods

 - (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];

}

@end

我的.h代码:

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController   <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)takePhoto:  (UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;

@end

这个问题真的很烦人,我真的很感激答案。代码是从照片库中选择图像。我有框架:FoundationUIKitCoreGraphics添加到我的项目中!

提前致谢!

2 个答案:

答案 0 :(得分:2)

相机在模拟器中不可用。 您已经进行了检查但未设置picker.sourceType属性:

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 } else {
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
[self.navigationController presentModalViewController:picker animated:YES];

答案 1 :(得分:1)

在info.plist中添加此内容

相机:

密钥:隐私 - 相机使用说明
价值:$(PRODUCT_NAME)相机使用 图片库:

密钥:隐私 - 照片库使用说明
价值:$(PRODUCT_NAME)照片使用