英文:
New PhotosPicker in SwiftUI can open camera instead of the photoLibrary
问题
在新的PhotosPicker中是否有办法让相机打开而不是打开相册?我记得在旧的UIImagePickerController中,用户可以在选择器中选择使用相机还是从图片中选择。
英文:
is there any way to make the new PhotosPicker open the camera instead of opening the photoLibrary, I remember in the old UIImagePickerController there was this option user could choose in the picker if use camera or select from picture.
ImagePikerCircular(im: im, dataImage: $dataImage)
.overlay(alignment: .bottomTrailing) {
PhotosPicker(selection: $im.imageSelection,
matching: .images,
photoLibrary: .shared()) {
Image(systemName: "pencil.circle.fill")
.symbolRenderingMode(.multicolor)
.font(.system(size: 30))
.foregroundColor(.accentColor)
}
.buttonStyle(.borderless)
.photosPickerStyle(.presentation)
}
答案1
得分: 1
没有,使用PhotosPicker没有打开相机的方法,您需要创建自己的选择对话框,当用户点击相机时,打开旧的UIImagePickerController,您可以创建一个UIViewControllerRepresentable如下:
struct CameraPickerView: UIViewControllerRepresentable {
private var sourceType: UIImagePickerController.SourceType = .camera
private let onImagePicked: (UIImage) -> Void
@Environment(\.presentationMode) private var presentationMode
public init(onImagePicked: @escaping (UIImage) -> Void) {
self.onImagePicked = onImagePicked
}
public func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.sourceType = self.sourceType
picker.delegate = context.coordinator
return picker
}
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
public func makeCoordinator() -> Coordinator {
Coordinator(
onDismiss: { self.presentationMode.wrappedValue.dismiss() },
onImagePicked: self.onImagePicked
)
}
final public class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
private let onDismiss: () -> Void
private let onImagePicked: (UIImage) -> Void
init(onDismiss: @escaping () -> Void, onImagePicked: @escaping (UIImage) -> Void) {
self.onDismiss = onDismiss
self.onImagePicked = onImagePicked
}
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let image = info[.originalImage] as? UIImage {
self.onImagePicked(image)
}
self.onDismiss()
}
public func imagePickerControllerDidCancel(_: UIImagePickerController) {
self.onDismiss()
}
}
}
然后,您可以使用fullScreenCover呈现选择器:
.fullScreenCover(isPresented: $showCameraPicker) {
CameraPickerView() { image in
// 对您的图像执行所需操作。
}
}
英文:
No, there is no way to open the camera using the PhotosPicker, you will need to create your own selection dialog and when the user taps the camera, open the old UIImagePickerController, you can create a UIViewControllerRepresentable like this:
struct CameraPickerView: UIViewControllerRepresentable {
private var sourceType: UIImagePickerController.SourceType = .camera
private let onImagePicked: (UIImage) -> Void
@Environment(\.presentationMode) private var presentationMode
public init(onImagePicked: @escaping (UIImage) -> Void) {
self.onImagePicked = onImagePicked
}
public func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.sourceType = self.sourceType
picker.delegate = context.coordinator
return picker
}
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
public func makeCoordinator() -> Coordinator {
Coordinator(
onDismiss: { self.presentationMode.wrappedValue.dismiss() },
onImagePicked: self.onImagePicked
)
}
final public class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
private let onDismiss: () -> Void
private let onImagePicked: (UIImage) -> Void
init(onDismiss: @escaping () -> Void, onImagePicked: @escaping (UIImage) -> Void) {
self.onDismiss = onDismiss
self.onImagePicked = onImagePicked
}
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let image = info[.originalImage] as? UIImage {
self.onImagePicked(image)
}
self.onDismiss()
}
public func imagePickerControllerDidCancel(_: UIImagePickerController) {
self.onDismiss()
}
}
}
Then, you can present the picker with a fullScreenCover:
.fullScreenCover(isPresented: $showCameraPicker) {
CameraPickerView() { image in
// Do what you want with your image.
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论