feat: 更新AppSettingView和ImagePicker组件以增强图片选择与预览体验

- 在AppSettingView中修复selectedImages的绑定,确保预览组件正确接收图片。
- 在ImagePreviewView中将images参数改为@Binding类型,提升数据流动性。
- 在ImagePickerWithPreviewView中使用.constant修饰符,优化预览逻辑。
- 在CameraPicker中添加相机控制显示和视图变换设置,提升用户体验。
- 在ImagePreviewView中添加加载状态提示,改善用户反馈。
This commit is contained in:
edwinQQQ
2025-07-26 09:55:23 +08:00
parent bc96cc47ff
commit a37d7c6eb8
4 changed files with 29 additions and 9 deletions

View File

@@ -73,7 +73,7 @@ struct AppSettingView: View {
}
.fullScreenCover(isPresented: $showPreview) {
ImagePreviewView(
images: selectedImages,
images: $selectedImages,
currentIndex: .constant(0),
onConfirm: {
print("[LOG] 预览确认,准备上传头像")
@@ -102,7 +102,10 @@ struct AppSettingView: View {
if let data = try? result.get(), let uiImage = UIImage(data: data) {
DispatchQueue.main.async {
tempImages.append(uiImage)
print("[LOG] 成功加载图片当前tempImages数量: \(tempImages.count)")
}
} else {
print("[LOG] 图片加载失败")
}
}
}
@@ -110,6 +113,7 @@ struct AppSettingView: View {
group.wait()
DispatchQueue.main.async {
isLoading = false
print("[LOG] 所有图片加载完成tempImages数量: \(tempImages.count)")
if tempImages.isEmpty {
errorMessage = "图片加载失败,请重试"
//
@@ -119,11 +123,13 @@ struct AppSettingView: View {
showActionSheet = false
print("[LOG] PhotosPicker图片加载失败弹出错误提示")
} else {
// selectedImages
selectedImages = tempImages
print("[LOG] PhotosPicker获得图片准备延迟进入预览图片数量: \(tempImages.count)")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
print("[LOG] selectedImages已设置数量: \(selectedImages.count)")
// 线showPreview
DispatchQueue.main.async {
showPreview = true
print("[LOG] PhotosPicker延迟后进入预览")
print("[LOG] showPreview已设置为true")
}
}
}

View File

@@ -15,6 +15,8 @@ public struct CameraPicker: UIViewControllerRepresentable {
picker.sourceType = .camera
picker.delegate = context.coordinator
picker.allowsEditing = false
picker.cameraViewTransform = .identity
picker.showsCameraControls = true
return picker
}
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
@@ -35,4 +37,4 @@ public struct CameraPicker: UIViewControllerRepresentable {
}
}
//
//

View File

@@ -142,7 +142,7 @@ private struct PreviewCoverModifier: ViewModifier {
set: { _ in }
)) {
ImagePreviewView(
images: previewImages,
images: .constant(previewImages),
currentIndex: .init(
get: { viewStore.inner.previewIndex },
set: { viewStore.send(.inner(.setPreviewIndex($0))) }

View File

@@ -1,13 +1,13 @@
import SwiftUI
public struct ImagePreviewView: View {
let images: [UIImage]
@Binding var images: [UIImage]
@Binding var currentIndex: Int
let onConfirm: () -> Void
let onCancel: () -> Void
public init(images: [UIImage], currentIndex: Binding<Int>, onConfirm: @escaping () -> Void, onCancel: @escaping () -> Void) {
self.images = images
public init(images: Binding<[UIImage]>, currentIndex: Binding<Int>, onConfirm: @escaping () -> Void, onCancel: @escaping () -> Void) {
self._images = images
self._currentIndex = currentIndex
self.onConfirm = onConfirm
self.onCancel = onCancel
@@ -29,6 +29,16 @@ public struct ImagePreviewView: View {
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: images.count > 1 ? .always : .never))
.frame(maxHeight: 400)
} else {
//
VStack {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.scaleEffect(1.5)
Text("加载图片中...")
.foregroundColor(.white)
.padding(.top, 16)
}
}
Spacer()
HStack(spacing: 24) {
@@ -48,6 +58,8 @@ public struct ImagePreviewView: View {
.background(Color.blue)
.cornerRadius(20)
}
.disabled(images.isEmpty)
.opacity(images.isEmpty ? 0.5 : 1.0)
}
.padding(.bottom, 40)
}