在 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
中不起作用
问题出在模拟器中还是代码中?
I have the method, that take photos from gallery or from the camera
-(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];
}
But when i run it on the simulator, code doesnt work. And it doesnt work in picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum
and picker.sourceType = UIImagePickerControllerSourceTypeCamera
Is the problem in the simulator or in the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
,请尝试此操作
如果您正在为 iPad 创建应用程序 。您必须在 popOver 控件中显示图库。
Try this,
If you are creating the app for iPad. You will have to present the gallery in a popOver control.
Swift 3/4/5 版本:
Swift 2 版本:
在模拟器中,您不能使用
cameraCaptureMode
和showsCameraControls
。Swift 3/4/5 verison:
Swift 2 version:
In simulator, you can't use
cameraCaptureMode
andshowsCameraControls
.在模拟器中,您的 picker.sourceType = UIImagePickerControllerSourceTypeCamera 将不会被调用,因为模拟器中没有可用的相机。检查源类型是否可用以避免崩溃也是一个好习惯。
In simulator your picker.sourceType = UIImagePickerControllerSourceTypeCamera wont be called as there is no camera available in simulator. Also its a good practice to check whether the source type is available to avoid crashes.
与上面的答案类似,但我发现这更容易。如果设备没有摄像头(如模拟器),则显示弹出警报。 Sam代码,不同用法:
Similarly to the above answers, but I found this easier. Show a pop up alert if the device doesn't have a camera (like the simulator). Sam code, different usage: