UIImageView Array ForEach迭代循环问题

UIImageView Array ForEach Iteration Loop problem

在开发我的 iOS 应用程序时,我在获取 UIImageViews 数组中的图像以对该特定图像执行操作时遇到了问题。无论我对 LazyVGrid 照片集中的哪张图片执行 LongPressGesture 操作,它始终适用于显示的第一张图片。我认为这可能是我数组中 ID 的问题,ForEach 循环无法正确识别每个元素,但我不知道如何解决它。

我将数组保存在 ProfileViewModel class:

@Published var userPicturesView: [UIImageView] = [UIImageView]()

我正在 ProfileView 中循环浏览它:

ScrollView() {
                            LazyVGrid(columns: Array(repeating: GridItem(), count: 3)) {
                                ForEach(0..<profileViewModel.userPicturesView.count, id: \.self) { imageIndex in
                                    if profileViewModel.userPicturesView[imageIndex].image != nil {
                                        Image(uiImage: profileViewModel.userPicturesView[imageIndex].image!)
                                            .resizable()
                                            .border(Color.black, width: 0.25)
                                            .frame(width: screenWidth * 0.34, height: screenHeight * 0.17)
                                            .onLongPressGesture {
                                                withAnimation {
                                                    self.shouldPresentEditActionSheet = true
                                                }
                                            }
                                            .actionSheet(isPresented: $shouldPresentEditActionSheet) {
                                                ActionSheet(title: Text("Edit selected"), message: nil, buttons: [
                                                    .default(Text("Set as profile picture"), action: {
                                                        withAnimation {
                                                            self.profilePictureImage = Image(uiImage: self.profileViewModel.userPicturesView[imageIndex].image!)
                                                        }
                                                    }),
                                                    .destructive(Text("Delete this photo"), action: {
                                                        withAnimation {
                                                            self.profileViewModel.deleteUserImage(imageIndex: imageIndex)
                                                        }
                                                    }),
                                                    .cancel()
                                                ])
                                            }
                                    }
                                }
                            }
                        }

我可以补充一点,我尝试通过添加包含我的图片的自定义结构来解决它:

struct PictureView: Identifiable {
    var id = UUID()
    var uiImageView: UIImageView
    
    init(uiImageView: UIImageView) {
        self.uiImageView = uiImageView
    }
}

然后在 ProfileViewModel 中创建一个结构数组,如下所示:

@Published var userPicturesView: [PictureView] = [PictureView]()

然后以这种方式更改 ProfileView:

ScrollView() {
                            LazyVGrid(columns: Array(repeating: GridItem(), count: 3)) {
                                ForEach(self.profileViewModel.userPicturesView) { userPictureView in
                                    if userPictureView.uiImageView.image != nil {
                                        Image(uiImage: userPictureView.uiImageView.image!)
                                            .resizable()
                                            .border(Color.black, width: 0.25)
                                            .frame(width: screenWidth * 0.34, height: screenHeight * 0.17)
                                            .onLongPressGesture {
                                                withAnimation {
                                                    self.shouldPresentEditActionSheet = true
                                                }
                                            }
                                            .actionSheet(isPresented: $shouldPresentEditActionSheet) {
                                                ActionSheet(title: Text("Edit selected"), message: nil, buttons: [
                                                    .default(Text("Set as profile picture"), action: {
                                                        withAnimation {
                                                            self.profilePictureImage = Image(uiImage: userPictureView.uiImageView.image!)
                                                        }
                                                    }),
                                                    .destructive(Text("Delete this photo"), action: {
                                                        withAnimation {
//                                                            self.profileViewModel.deleteUserImage(imageIndex: imageIndex)
                                                        }
                                                    }),
                                                    .cancel()
                                                ])
                                            }
                                    }
                                }
                            }
                        }

但这带来了同样的结果。 我们将不胜感激。

使用 actionSheet(item:) 而不是 actionSheet(isPresented:) 将允许您访问所选项目。在 iOS 14+ 中,isPresented 先渲染以前的值存在问题。这是您的代码的简化版本:

struct ContentView: View {
    let pictureViews : [PictureView] = []
    @State private var selectedItem : PictureView? = nil
    
    var body: some View {
        ForEach(pictureViews) { pictureView in
            Image(uiImage: pictureView.uiImageView.image!)
                .onLongPressGesture {
                    selectedItem = pictureView
                }
        }.actionSheet(item: $selectedItem) { item in
            ActionSheet(title: Text("Edit selected"), message: nil, buttons: [
                .default(Text("Set as profile picture"), action: {
                    withAnimation {
                        //use `item` here
                    }
                }),
                .destructive(Text("Delete this photo"), action: {
                    withAnimation {
                        //use `item` here
                    }
                }),
                .cancel()
            ])
        }
    }
}