gpt4 book ai didi

SwiftUI 全屏 UIImagePickerController(相机)

转载 作者:行者123 更新时间:2023-12-04 01:28:53 30 4
gpt4 key购买 nike

我赠送一个 UIImagePickerController在我的应用程序中通过在 sheet 中呈现逻辑修饰符。简而言之,以下三种类型处理显示和关闭 UIImagePickerController 的实例。内部 UIViewControllerRepresentable类型,按预期工作:

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()
}

}

我遇到的问题是相机 View 是以模态呈现的,它没有覆盖整个屏幕。这会导致 UIImagePickerController当相机是来源时,有时会出现布局损坏的情况,就好像相机不是以这种方式呈现的一样。设置 imagePicker.modalPresentationStyle = .fullScreen不会导致全屏演示。

Screenshot of UIImagePickerController with a camera source

如何以全屏布局显示相机,使其不会以卡片式的演示方式出现?

最佳答案

结合@LaX 和@GrandSteph 的答案,我有:

.fullScreenCover(isPresented: $activeSheet, content: {
ImagePicker(image: $inputImage, sourceType: .camera)
.edgesIgnoringSafeArea(.all)
})

关于SwiftUI 全屏 UIImagePickerController(相机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61375748/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com