gpt4 book ai didi

swift - 如何在 SwiftUI 中从图库中选择图像

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

我试图制作一个符合 UIViewRepresentable 并实现 makeUIView 和 updateUIView 的 SwiftUI 类。
但无法完成它。这是代码:

代码:

struct ImagePicker : UIViewRepresentable {

@Binding var image: UIImage

class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@Binding var image: UIImage

init(image: Binding<UIImage>) {
$image = image
}

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


if let possibleImage = info[.editedImage] as? UIImage {
image = possibleImage
} else if let possibleImage = info[.originalImage] as? UIImage {
image = possibleImage
} else {

}
}
}

func makeCoordinator() -> Coordinator {
return Coordinator(image: $image)
}

func makeUIView(context: UIViewRepresentableContext<ImagePicker>) -> UIImageView {
let imageview = UIImageView()
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return imageview
}

func updateUIView(_ uiView: UIImageView,
context: UIViewRepresentableContext<ImagePicker>) {
uiView.image = image
}

}

我尝试使用 imagePicker 作为控件但无法使用。

最佳答案

我正在使用 presentationMode在这里,检查它是否呈现的 View ?

通过使用它,您可以 dismiss() UIImagePickerController .

SwiftUI 预览的示例代码:

import SwiftUI

struct ContentView: View {

@State var isShowPicker: Bool = false
@State var image: Image? = Image("placeholder")

var body: some View {
NavigationView {
ZStack {
VStack {
image?
.resizable()
.scaledToFit()
.frame(height: 320)
Button(action: {
withAnimation {
self.isShowPicker.toggle()
}
}) {
Image(systemName: "photo")
.font(.headline)
Text("IMPORT").font(.headline)
}.foregroundColor(.black)
Spacer()
}
}
.sheet(isPresented: $isShowPicker) {
ImagePicker(image: self.$image)
}
.navigationBarTitle("Pick Image")
}
}
}


struct ImagePicker: UIViewControllerRepresentable {

@Environment(\.presentationMode)
var presentationMode

@Binding var image: Image?

class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@Binding var presentationMode: PresentationMode
@Binding var image: Image?

init(presentationMode: Binding<PresentationMode>, image: Binding<Image?>) {
_presentationMode = presentationMode
_image = image
}

func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
image = Image(uiImage: uiImage)
presentationMode.dismiss()

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
presentationMode.dismiss()
}

}

func makeCoordinator() -> Coordinator {
return Coordinator(presentationMode: presentationMode, image: $image)
}

func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}

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

}

}


struct ContentView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ContentView()
}
}
}

关于swift - 如何在 SwiftUI 中从图库中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57110290/

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