在iPhone模拟器上使用UIImagePickerController
我有方法,从画廊或从相机拍照
-(IBAction) getPhoto:(id) sender { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; if((UIButton *) sender == choosePhotoBtn) { picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } else { picker.sourceType = UIImagePickerControllerSourceTypeCamera; } [self presentModalViewController:picker animated:YES]; }
但是,当我在模拟器上运行它,代码不起作用。 它不工作在picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum
和picker.sourceType = UIImagePickerControllerSourceTypeCamera
是在模拟器或代码中的问题?
尝试这个,
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } else { picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } [self.navigationController presentModalViewController:picker animated:NO];
如果您正在为iPad创build应用程序。 您将不得不在popOver控件中呈现该库。
在模拟器中,您的picker.sourceType = UIImagePickerControllerSourceTypeCamera将不会被调用,因为在模拟器中没有可用的相机。 另外它是检查源types是否可用以避免崩溃的一个好习惯。
#import <MobileCoreServices/UTCoreTypes.h> …. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init]; imagePickerCamera.delegate = self; imagePickerCamera.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil]; imagePickerCamera.allowsEditing = YES; imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePickerCamera animated:YES completion:nil]; } else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init]; imagePickerAlbum.delegate = self; imagePickerAlbum.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil]; imagePickerAlbum.allowsEditing = YES; imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:imagePickerAlbum animated:YES completion:nil]; }
Swift 2.2版本:
if UIImagePickerController.isSourceTypeAvailable(.Camera) { picker!.sourceType = .Camera } else { picker!.sourceType = .SavedPhotosAlbum // or .PhotoLibrary }
Swift 3.0版本:
if UIImagePickerController.isSourceTypeAvailable(.camera) { picker.sourceType = .camera } else { picker.sourceType = .savedPhotosAlbum // or .photoLibrary }
在模拟器中,你也不应该设置cameraCaptureMode
也不能显示showsCameraControls
。
类似于上面的答案,但我发现这更容易。 如果设备没有相机(如模拟器),则显示popup警报。 萨姆码,不同用法:
//button if to take a photo - (IBAction)takePhoto:(id)sender { //checks if device has a camera if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You don't have a camera for this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; //shows above alert if there's no camera [noCameraAlert show]; } //otherwise, show a modal for taking a photo else { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePicker animated:YES completion:NULL]; } }