[英]How to let user toggle between front facing and rear camera in UIImagePicker?
How to let user toggle between front facing and rear camera in UIImagePicker?如何让用户在 UIImagePicker 中的前置摄像头和后置摄像头之间切换?
-(void)takePhoto {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
#if TARGET_IPHONE_SIMULATOR
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
imagePickerController.editing = YES;
imagePickerController.delegate = (id)self;
[self presentModalViewController:imagePickerController animated:YES];
}
More than happy to delete this if necessary, just trying to help....如有必要,非常乐意删除此内容,只是想提供帮助....
I just tried my Swift-written app that uses UIImagePickerController
, with this code that I haven';t touched since 2016. It looks similar to your code - except that allowsEditing = false
, which to you means editing = NO
.我刚刚尝试了使用
UIImagePickerController
的 Swift 编写的应用程序,其中包含自 2016 年以来我没有接触过的代码。它看起来与您的代码相似 - 除了allowsEditing = false
,这对您来说意味着 editor editing = NO
。 (I also check for a rear-facing camera to make sure it's not a simulator running the code.) To me, it looks like UIImagePickerController
automatically gives the user a way to switch to the front-facing camera. (我还检查了后置摄像头,以确保它不是运行代码的模拟器。)对我来说,
UIImagePickerController
似乎自动为用户提供了一种切换到前置摄像头的方法。
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerController.SourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
Have you tried setting editing to NO?您是否尝试将编辑设置为 NO?
can't be told enough.说不完。 When writing code in Xcode you can right-click on any class, enum, method etc. to browse to the definition and read on.
在 Xcode 中编写代码时,您可以右键单击任何 class、枚举、方法等以浏览到定义并继续阅读。 So when you right-click on
UIImagePickerControllerSourceTypeCamera
and "Jump to Definition" you will end up reading UIKit/UIImagePickerController.h因此,当您右键单击
UIImagePickerControllerSourceTypeCamera
并“跳转到定义”时,您最终将阅读 UIKit/UIImagePickerController.h
Protocol <UIImagePickerControllerDelegate>
knows of an Enum with name UIImagePickerControllerCameraDevice
that has two entrys, UIImagePickerControllerCameraDeviceRear
or UIImagePickerControllerCameraDeviceFront
协议
<UIImagePickerControllerDelegate>
知道一个名为UIImagePickerControllerCameraDevice
的枚举,它有两个条目, UIImagePickerControllerCameraDeviceRear
或UIImagePickerControllerCameraDeviceFront
and later on the protocol tells, you have a后来协议告诉你,你有一个
@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice;
available.可用的。
Meaning, you can set意思是,你可以设置
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
and also defined in the protocol which your imagePickerController is following并且还在您的 imagePickerController 遵循的协议中定义
@property(nonatomic) BOOL showsCameraControls;
so you can这样你就可以
imagePickerController.showsCameraControls = YES;
makes it easy for you and the users know how to handle that interface.使您和用户知道如何处理该界面变得容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.