gpt4 book ai didi

swift - 在风景中使用相机的图像选择器无法正常工作 (SwiftUI)

转载 作者:行者123 更新时间:2023-12-04 17:17:29 25 4
gpt4 key购买 nike

我的图像选择器在两个方向都可以使用照片库,但是使用相机时它无法正确调整为横向。
这是它的外观:
景观:
Problematic Camera View
肖像:
Camera View That Looks Normal
这是我的代码:
File1, UIViewControllerRepresentable:

import UIKit
import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {

var sourceType: UIImagePickerController.SourceType = .photoLibrary

@Binding var selectedImage: UIImage
@Environment(\.presentationMode) private var presentationMode

func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {

let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.sourceType = sourceType
imagePicker.delegate = context.coordinator

return imagePicker
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {

}

func makeCoordinator() -> Coordinator {
Coordinator(self)
}



final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


var parent: ImagePicker

init(_ parent: ImagePicker) {
self.parent = parent
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
parent.selectedImage = image
}

parent.presentationMode.wrappedValue.dismiss()
}
}
}
文件 2,使用它:
.fullScreenCover(isPresented: $isShowPhotoLibrary) {
ImagePicker(sourceType: .camera, selectedImage: self.$image)
}
我并不固定使用 fullScreenCover,只是这样做是因为它至少部分有效。
如果有人可以提供帮助,我将不胜感激。

最佳答案

可能有一个解决方法,但根据 Apple 文档:
“UIImagePickerController 类仅支持纵向模式......”
https://developer.apple.com/documentation/uikit/uiimagepickercontroller
也许你可以使用:

let theCamera = AVCaptureDevice.default(for: .video)
这很有效。

关于swift - 在风景中使用相机的图像选择器无法正常工作 (SwiftUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68321130/

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