在 iphone 模拟器上使用 UIImagePickerController
Posted
技术标签:
【中文标题】在 iphone 模拟器上使用 UIImagePickerController【英文标题】:Use the UIImagePickerController on a iphone simulator 【发布时间】:2012-02-07 08:14:55 【问题描述】:我有方法,从画廊或相机拍照
-(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
中不起作用
是模拟器的问题还是代码的问题?
【问题讨论】:
这里是答案:***.com/questions/833210/… 您使用的是 iPhone 模拟器还是 iPad 模拟器。 Iphone 模拟器。我将 'UIImagePickerControllerSourceTypeSavedPhotosAlbum' 更改为 'UIImagePickerControllerSourceTypePhotoLibrary' 但库是空的,应该在哪里添加照片,然后在应用程序中使用它们 是的,因为你使用的是模拟器。 【参考方案1】:试试这个,
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
else
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.navigationController presentModalViewController:picker animated:NO];
如果您正在为 iPad 创建应用程序。您必须在 popOver 控件中显示图库。
【讨论】:
[self.navigationController presentModalViewController:picker animated:NO] - 不起作用,但 [self presentModalViewController:picker animated:YES] 没问题。关于 sourceType 我也有同样的问题。为什么 UIImagePickerControllerSourceTypePhotoLibrary 是空的?我应该如何在运行应用程序之前添加照片【参考方案2】:Swift 3/4/5 版本:
if UIImagePickerController.isSourceTypeAvailable(.camera)
picker.sourceType = .camera
else
picker.sourceType = .savedPhotosAlbum // or .photoLibrary
Swift 2 版本:
if UIImagePickerController.isSourceTypeAvailable(.Camera)
picker.sourceType = .Camera
else
picker.sourceType = .SavedPhotosAlbum // or .PhotoLibrary
在模拟器中,你不能使用cameraCaptureMode
和showsCameraControls
。
【讨论】:
【参考方案3】:在模拟器中,您的 picker.sourceType = UIImagePickerControllerSourceTypeCamera 不会被调用,因为模拟器中没有可用的相机。检查源类型是否可用以避免崩溃也是一个好习惯。
#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];
【讨论】:
查看此链接 - developer.apple.com/library/ios/#documentation/UIKit/Reference/…【参考方案4】:与上述答案类似,但我发现这更容易。如果设备没有摄像头(如模拟器),则显示弹出警报。 Sam 代码,不同用法:
//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];
【讨论】:
以上是关于在 iphone 模拟器上使用 UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章
在 iphone 模拟器上使用 UIImagePickerController
iPhone - UITableView 使用 plist 填充在模拟器中有效,但在 iDevice 上无效