数组中的 ImagePicker
ImagePicker in array
我想在数组的帮助下制作动态图像 selection,但我遇到的问题是我 select 第一个位置并且照片加载在第一个位置, 但如果我 select 另一个位置,图像总是加载在第一个位置。
我在foreach里面占用了一个.sheet,值得一提的是我要实现的是图片数量,不是固定大小
代码:
import SwiftUI
struct Research {
var id: Int
var name: String
var date: String
var aux: Int
var imagen1: UIImage
var imagen2: UIImage
var imagen3: UIImage
var imagen4: UIImage
var imagen5: UIImage
var imagen6: UIImage
}
struct ContentView: View {
@State var dataResearch : [Research] = []
@State var aux: Int = 0
@State var showPicker: Bool = false // I imagine that this variable must be unique per element
@State var sourceType: UIImagePickerController.SourceType = .photoLibrary
var body: some View {
VStack{
ForEach($dataResearch, id: \.aux) { $i in
VStack{
Image(uiImage: i.imagen1)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
showPicker = true
}
Image(uiImage: i.imagen2)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
showPicker = true
}
}
.sheet(isPresented: $showPicker) {
ImagePicker(sourceType: sourceType, selectedImage: $i.imagen1)
.ignoresSafeArea()
}
}
}
.onAppear(){
getRequest()
}
}
func getRequest(){
let image1: UIImage = UIImage(named: "default_image")!
let image2: UIImage = UIImage(named: "default_image")!
let image3: UIImage = UIImage(named: "default_image")!
let image4: UIImage = UIImage(named: "default_image")!
let image5: UIImage = UIImage(named: "default_image")!
let image6: UIImage = UIImage(named: "default_image")!
let row = Research(id: 0, name: "", date: "", aux: aux, imagen1: image1, imagen2: image2, imagen3: image3, imagen4: image4, imagen5: image5, imagen6: image6)
dataResearch.append(row)
aux = aux + 1
}
}
图像选择器
import UIKit
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
var sourceType: UIImagePickerController.SourceType = .photoLibrary
@Binding var selectedImage: UIImage
//@Binding var selectedImage: SelectImage
@Environment(\.presentationMode) private var presentationMode
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
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
print("aqui vemos-- ", parent)
print("aqui vemos-- ", parent.selectedImage)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
parent.selectedImage = image
//var row1 = SelectImage(id: 0, uiImage: image ,name:"")
//parent.selectedImage.uiImage = image
}
parent.presentationMode.wrappedValue.dismiss()
}
}
}
由于 Research
的结构以及它有很多图像字段(不在数组中)这一事实,这有点复杂。我建议您的模型有可能使用重构,但由于我没有关于最终目标是什么的任何信息,所以我还不知道具体涉及什么。
使用您当前的代码,您可以设置一个选定的项目,然后设置一个额外的变量来确定它应该绑定到哪个 imagen(X)
属性:
struct ContentView: View {
@State private var dataResearch : [Research] = []
@State private var aux: Int = 0
@State private var pickerItem : Binding<Research>? // the item from ForEach
private enum SelectedImage {
case imagen1, imagen2
}
@State private var selectedImage : SelectedImage? //which image gets selected
var body: some View {
VStack{
ForEach($dataResearch, id: \.aux) { $i in
VStack{
Image(uiImage: i.imagen1)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
pickerItem = $i
selectedImage = .imagen1
}
Image(uiImage: i.imagen2)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
pickerItem = $i
selectedImage = .imagen2
}
}
}
.sheet(item: $pickerItem) { [selectedImage] item in
VStack {
if let selectedImage = selectedImage {
switch selectedImage {
case .imagen1:
ImagePicker(selectedImage: item.imagen1)
case .imagen2:
ImagePicker(selectedImage: item.imagen2)
}
}
}
.ignoresSafeArea()
}
}
.onAppear(){
getRequest()
}
}
func getRequest(){
let image1: UIImage = UIImage(systemName: "pencil")!
let image2: UIImage = UIImage(systemName: "pencil")!
let image3: UIImage = UIImage(systemName: "pencil")!
let image4: UIImage = UIImage(systemName: "pencil")!
let image5: UIImage = UIImage(systemName: "pencil")!
let image6: UIImage = UIImage(systemName: "pencil")!
let row = Research(id: 0, name: "", date: "", aux: aux, imagen1: image1, imagen2: image2, imagen3: image3, imagen4: image4, imagen5: image5, imagen6: image6)
dataResearch.append(row)
aux = aux + 1
}
}
我想在数组的帮助下制作动态图像 selection,但我遇到的问题是我 select 第一个位置并且照片加载在第一个位置, 但如果我 select 另一个位置,图像总是加载在第一个位置。
我在foreach里面占用了一个.sheet,值得一提的是我要实现的是图片数量,不是固定大小
代码:
import SwiftUI
struct Research {
var id: Int
var name: String
var date: String
var aux: Int
var imagen1: UIImage
var imagen2: UIImage
var imagen3: UIImage
var imagen4: UIImage
var imagen5: UIImage
var imagen6: UIImage
}
struct ContentView: View {
@State var dataResearch : [Research] = []
@State var aux: Int = 0
@State var showPicker: Bool = false // I imagine that this variable must be unique per element
@State var sourceType: UIImagePickerController.SourceType = .photoLibrary
var body: some View {
VStack{
ForEach($dataResearch, id: \.aux) { $i in
VStack{
Image(uiImage: i.imagen1)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
showPicker = true
}
Image(uiImage: i.imagen2)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
showPicker = true
}
}
.sheet(isPresented: $showPicker) {
ImagePicker(sourceType: sourceType, selectedImage: $i.imagen1)
.ignoresSafeArea()
}
}
}
.onAppear(){
getRequest()
}
}
func getRequest(){
let image1: UIImage = UIImage(named: "default_image")!
let image2: UIImage = UIImage(named: "default_image")!
let image3: UIImage = UIImage(named: "default_image")!
let image4: UIImage = UIImage(named: "default_image")!
let image5: UIImage = UIImage(named: "default_image")!
let image6: UIImage = UIImage(named: "default_image")!
let row = Research(id: 0, name: "", date: "", aux: aux, imagen1: image1, imagen2: image2, imagen3: image3, imagen4: image4, imagen5: image5, imagen6: image6)
dataResearch.append(row)
aux = aux + 1
}
}
图像选择器
import UIKit
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
var sourceType: UIImagePickerController.SourceType = .photoLibrary
@Binding var selectedImage: UIImage
//@Binding var selectedImage: SelectImage
@Environment(\.presentationMode) private var presentationMode
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
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
print("aqui vemos-- ", parent)
print("aqui vemos-- ", parent.selectedImage)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
parent.selectedImage = image
//var row1 = SelectImage(id: 0, uiImage: image ,name:"")
//parent.selectedImage.uiImage = image
}
parent.presentationMode.wrappedValue.dismiss()
}
}
}
由于 Research
的结构以及它有很多图像字段(不在数组中)这一事实,这有点复杂。我建议您的模型有可能使用重构,但由于我没有关于最终目标是什么的任何信息,所以我还不知道具体涉及什么。
使用您当前的代码,您可以设置一个选定的项目,然后设置一个额外的变量来确定它应该绑定到哪个 imagen(X)
属性:
struct ContentView: View {
@State private var dataResearch : [Research] = []
@State private var aux: Int = 0
@State private var pickerItem : Binding<Research>? // the item from ForEach
private enum SelectedImage {
case imagen1, imagen2
}
@State private var selectedImage : SelectedImage? //which image gets selected
var body: some View {
VStack{
ForEach($dataResearch, id: \.aux) { $i in
VStack{
Image(uiImage: i.imagen1)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
pickerItem = $i
selectedImage = .imagen1
}
Image(uiImage: i.imagen2)
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.edgesIgnoringSafeArea(.all)
.onTapGesture(){
pickerItem = $i
selectedImage = .imagen2
}
}
}
.sheet(item: $pickerItem) { [selectedImage] item in
VStack {
if let selectedImage = selectedImage {
switch selectedImage {
case .imagen1:
ImagePicker(selectedImage: item.imagen1)
case .imagen2:
ImagePicker(selectedImage: item.imagen2)
}
}
}
.ignoresSafeArea()
}
}
.onAppear(){
getRequest()
}
}
func getRequest(){
let image1: UIImage = UIImage(systemName: "pencil")!
let image2: UIImage = UIImage(systemName: "pencil")!
let image3: UIImage = UIImage(systemName: "pencil")!
let image4: UIImage = UIImage(systemName: "pencil")!
let image5: UIImage = UIImage(systemName: "pencil")!
let image6: UIImage = UIImage(systemName: "pencil")!
let row = Research(id: 0, name: "", date: "", aux: aux, imagen1: image1, imagen2: image2, imagen3: image3, imagen4: image4, imagen5: image5, imagen6: image6)
dataResearch.append(row)
aux = aux + 1
}
}