问题描述
我在我的应用程序中展示了一个 UIImagePickerController
,方法是在 sheet
修饰符中展示它的逻辑.简而言之,以下三种类型处理显示和关闭 UIViewControllerRepresentable
类型内的 UIImagePickerController
实例,它按预期工作:
I present a UIImagePickerController
within my application by presenting it with logic inside of a sheet
modifier. In short, the following three types handle displaying and dismissing a instance of UIImagePickerController
inside of a UIViewControllerRepresentable
type, which works as expected:
struct DetailsView: View {
enum Sheet: Hashable, Identifiable {
case takePhoto
var id: Int { hashValue }
}
@State private var activeSheet: Sheet?
var body: some View {
Text("Hello, World!")
.sheet(item: $activeSheet) { (sheet) in self.view(for: sheet) }
}
private func view(for sheet: Sheet) -> some View {
switch sheet {
case .takePhoto: return PhotoSelectionView(showImagePicker: .init(get: { sheet == .takePhoto }, set: { (show) in self.activeSheet = show ? .takePhoto : nil }), image: $selectedImage, photoSource: .camera).edgesIgnoringSafeArea(.all)
}
}
}
struct ImagePicker: UIViewControllerRepresentable {
@Binding var isShown: Bool
@Binding var image: Image?
let photoSource: PhotoSource
func makeCoordinator() -> ImagePickerCoordinator {
return ImagePickerCoordinator(isShown: $isShown, selectedImage: $image)
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
if photoSource == .camera, UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
}
imagePicker.delegate = context.coordinator
return imagePicker
}
}
class ImagePickerCoordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@Binding private var isShown: Bool
@Binding private var selectedImage: Image?
init(isShown: Binding<Bool>, selectedImage: Binding<Image?>) {
_isShown = isShown
_selectedImage = selectedImage
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// handle photo selection
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss()
}
}
我遇到的问题是相机视图是以模态呈现的,它没有覆盖整个屏幕.这会导致 UIImagePickerController
有时在相机是源时看起来布局损坏,就好像相机不是以这种方式呈现的一样.设置 imagePicker.modalPresentationStyle = .fullScreen
不会导致全屏演示.
The issue that I am having is that the camera view is presented modally, which doesn't cover the entire screen. This causes the UIImagePickerController
to appear to have broken layouts at times when the camera is the source, as if the camera was not made to be presented in this way. Setting imagePicker.modalPresentationStyle = .fullScreen
does not result in a full-screen presentation.
如何以全屏布局显示摄像头,使其不以卡片式呈现方式显示?
How can I display the camera in a full-screen layout so that it does not appear in the card-like presentation style?
推荐答案
结合@LaX 和@GrandSteph 的回答,我有:
combining @LaX and @GrandSteph's answers, I have:
.fullScreenCover(isPresented: $activeSheet, content: {
ImagePicker(image: $inputImage, sourceType: .camera)
.edgesIgnoringSafeArea(.all)
})
这篇关于SwiftUI 全屏 UIImagePickerController(相机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!