feat: 更新AppSettingView和ImagePicker组件以增强图片选择与预览体验
- 在AppSettingView中修复selectedImages的绑定,确保预览组件正确接收图片。 - 在ImagePreviewView中将images参数改为@Binding类型,提升数据流动性。 - 在ImagePickerWithPreviewView中使用.constant修饰符,优化预览逻辑。 - 在CameraPicker中添加相机控制显示和视图变换设置,提升用户体验。 - 在ImagePreviewView中添加加载状态提示,改善用户反馈。
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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 {
|
||||
}
|
||||
}
|
||||
|
||||
// 可选:如需自定义相册选择器,可扩展此文件
|
||||
// 可选:如需自定义相册选择器,可扩展此文件
|
||||
|
@@ -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))) }
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user