我正在使用相机捕获应用程序的图像。触摸照片按钮后, 只能以正方形格式保存照片。我想保留4:3格式 较旧的相机,并最终允许用户根据需要选择16:9。我没去过 能够找到任何代码以编程方式选择纵横比。我只有广场。我什至 尝试在设备的“摄像机设置”中锁定“摄像机模式”。幸运的是 没用。
这是一个SwiftUI应用。在下面的CameraViewController中,如果我删除allowEditing 选项,选择器将冻结-“使用照片”或“重新拍摄”按钮均不起作用。的 应用程序不会崩溃,只是视图被冻结。我可以向下滑动视图以 退出并继续使用该应用。
我从下面的按钮中调用下面的PhotoCaptureView,该按钮为 等同于主/详细信息。
PhotoCaptureView:
import SwiftUI
struct PhotoCaptureView: View {
@EnvironmentObject var cameraSettings: CameraSettings
@Binding var photos: [UIImage]
var body: some View {
return VStack {
CameraViewController(photos: $photos)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}.edgesIgnoringSafeArea(.all)
}
}
模型视图CameraViewController:
import UIKit
import SwiftUI
import MobileCoreServices
import AVFoundation
struct CameraViewController: UIViewControllerRepresentable {
@EnvironmentObject var cameraSettings: CameraSettings
@Binding var photos: [UIImage]
func updateUIViewController(_ uiViewContyroller: UIImagePickerController, context: Context) {
//no code here
}
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraViewController>) -> UIImagePickerController {
let vc = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
vc.sourceType = .camera
vc.allowsEditing = true
vc.delegate = context.coordinator
return vc
}
return UIImagePickerController()
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var parent: CameraViewController
init(_ imagePickerController: CameraViewController) {
self.parent = imagePickerController
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else {
print("no image found")
return
}
print("Image taken successfully, with size \(image.size)")
parent.photos.append(image)
picker.dismiss(animated: true) {
print("in dismiss after successful picture")
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true) {
print("in did cancel")
}
}
private func pickerController(_ controller: UIImagePickerController, didSelect image: UIImage?) {
controller.dismiss(animated: true, completion: nil)
}
}
}
CameraSettings只是确定是否提供相机选项的傻瓜。
class CameraSettings: ObservableObject {
@Published var isAuthorized: Bool = false
@Published var isCameraAvailable: Bool = false
}
Xcode 11.1(11A1027)我正在使用iPhone X iOS 13.1.2
任何指导将不胜感激。
答案 0 :(得分:1)
didFinishPickingMediaWithInfo的信息设置需要指定“ originalImage”,以避免仅显示正方形图片。但是,即使使用已编辑的版本,似乎您也只能移动图像-不能更改外观。
将上面的信息行更改为: 守卫让图像=信息[.originalImage]作为? UIImage其他{
然后设置: vc.allowsEditing = false
重要的是,文档告诉我:“ UIImagePickerController类仅支持纵向模式。”