2015-09-09 86 views
1

我的iPhone 5模拟器崩溃,当我试图打开一个UIImagePickerController模拟器崩溃时运行Xcode中的UIImagePickerController 7

我的.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添加到我的项目!

在此先感谢!

+3

什么是控制台中的错误? –

+0

是你碰到'takePhoto'时发生崩溃的代码吗? –

+0

你解决了这个问题吗?你可以发布控制台的日志吗? – Loegic

回答

2

相机在模拟器中不可用。 你已经取得的检查,但你没有设置picker.sourceType属性:

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

我从UIImagePickerControllerSourceTypePhotoLibrary改变它UIImagePickerControllerSourceTypeSavedPhotosAlbum

1

在您的info.plist

相机添加这个固定:

注:隐私 - 相机使用说明
值:$(PRODUCT_NAME)ca梅拉使用 图片库:

重点:隐私 - 图片库使用情况说明
价值:$(PRODUCT_NAME)的照片使用

相关问题