2015-09-09 91 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)的照片使用

相關問題