diff --git a/kplayer/detail/EditItemView.swift b/kplayer/detail/EditItemView.swift index 19d9e7f..95ce039 100644 --- a/kplayer/detail/EditItemView.swift +++ b/kplayer/detail/EditItemView.swift @@ -70,31 +70,7 @@ struct EditItemView: View { var snap = true @State - var processedImageURL: String? - - @State - var isProcessing = false - - @State - var showProcessedImage = false - - @State - var loadedImage: UIImage? - - @State - var imageLoadError: String? - - @State - var imageScale: CGFloat = 1.0 - - @State - var imageOffset: CGSize = .zero - - @State - var lastScale: CGFloat = 1.0 - - @State - var lastOffset: CGSize = .zero + var faceModel: SPhotoModel? = nil var len: Double @@ -200,136 +176,14 @@ struct EditItemView: View { UITableView.appearance().backgroundColor = .systemBackground } .frame(height: 800, alignment: .top) - .sheet(isPresented: $showProcessedImage) { - NavigationView { - VStack { - if let image = loadedImage { - GeometryReader { geometry in - Image(uiImage: image) - .resizable() - .aspectRatio(contentMode: .fit) - .frame(width: geometry.size.width, height: geometry.size.height) - .scaleEffect(imageScale) - .offset(imageOffset) - .gesture( - SimultaneousGesture( - MagnificationGesture() - .onChanged { value in - let delta = value / lastScale - lastScale = value - let newScale = imageScale * delta - imageScale = min(max(newScale, 1.0), 5.0) - } - .onEnded { value in - lastScale = 1.0 - }, - DragGesture(minimumDistance: 0) - .onChanged { value in - imageOffset = CGSize( - width: lastOffset.width + value.translation.width, - height: lastOffset.height + value.translation.height - ) - } - .onEnded { value in - lastOffset = imageOffset - } - ) - ) - .onTapGesture(count: 2) { - // Double tap to reset zoom - withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) { - imageScale = 1.0 - lastScale = 1.0 - imageOffset = .zero - lastOffset = .zero - } - } - } - } else if let error = imageLoadError { - VStack(spacing: 20) { - Image(systemName: "exclamationmark.triangle") - .font(.system(size: 60)) - .foregroundColor(.orange) - Text("Failed to load image") - .font(.headline) - Text(error) - .font(.caption) - .foregroundColor(.secondary) - if let imageURL = processedImageURL { - Text("URL: \(imageURL)") - .font(.caption2) - .foregroundColor(.secondary) - } - } - .padding() - } else { - ProgressView("Loading image...") - .frame(maxWidth: .infinity, maxHeight: .infinity) - } - } - .navigationTitle("Processed Image") - .navigationBarTitleDisplayMode(.inline) - .toolbar { - ToolbarItem(placement: .navigationBarLeading) { - if loadedImage != nil && imageScale != 1.0 { - Button("Reset") { - withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) { - imageScale = 1.0 - lastScale = 1.0 - imageOffset = .zero - lastOffset = .zero - } - } - } - } - ToolbarItem(placement: .navigationBarTrailing) { - Button("Done") { - showProcessedImage = false - loadedImage = nil - imageLoadError = nil - // Reset zoom state - imageScale = 1.0 - lastScale = 1.0 - imageOffset = .zero - lastOffset = .zero - } - } - } - .task { - if let imageURL = processedImageURL { - await loadImageFromURL(imageURL) - } - } + .sheet(isPresented: Binding(get: { faceModel != nil }, set: { if !$0 { faceModel = nil } })) { + if let fm = faceModel { + SPhotoView(model: fm) } } //Spacer() } - func loadImageFromURL(_ urlString: String) async { - guard let url = URL(string: urlString) else { - imageLoadError = "Invalid URL" - return - } - - do { - let (data, _) = try await URLSession.shared.data(from: url) - if let image = UIImage(data: data) { - await MainActor.run { - loadedImage = image - imageLoadError = nil - } - } else { - await MainActor.run { - imageLoadError = "Failed to create image from data" - } - } - } catch { - await MainActor.run { - imageLoadError = "Failed to load image: \(error.localizedDescription)" - } - } - } - func faceSelectedItem(_ name: String) { let path = item.fullPath.replacing("/srv/samba/ren", with: "z:") let outpath1 = path.replacing("/", with: "") @@ -342,42 +196,25 @@ struct EditItemView: View { Task { - isProcessing = true - processedImageURL = nil - - if (snap) { + if snap { let imagePath = item.imageUrlAbsolute.replacing("http://linkstation:8089/ren", with: "z:").replacing("_thumb", with: "") - print(imagePath) - print(item.thumbUrlAbsolute) - - let outimagepath = "z:/cut/snapshots/" + name + "/" + imagePath.substringStartingFrom(2).replacing("/", with: "") - do { - let response = try await FaceManager.sharedInstance.processImage(inputImagePath: imagePath, sourceFacePath: "benchmark/" + name + ".jpg", outputPath: outimagepath) - - //if response.success, let outputPath = response.outputPath { - // Convert Windows path back to HTTP URL - let httpURL = outimagepath.replacing("z:", with: "http://linkstation:8089/ren").replacing("\\", with: "/") - processedImageURL = httpURL - print("Processed image URL: \(httpURL)") - - // Show the sheet with the processed image - - - showProcessedImage = true - isProcessing = false + let data = try await FaceManager.sharedInstance.processOneImage(inputImage: imagePath, sourceFace: "benchmark/" + name + ".jpg") + if let img = UIImage(data: data) { + let fm = SPhotoModel(allItems: [item]) + fm.timer?.invalidate() + fm.image = img + await MainActor.run { faceModel = fm } + } } catch { print("Error processing image: \(error)") - isProcessing = false } } else { do { try await FaceManager.sharedInstance.processVideo(inputVideoPath: path, sourceFacePath: "benchmark/" + name + ".jpg", outputPath: outpath) delegate.cancelEdit() - isProcessing = false } catch { print("Error processing video: \(error)") - isProcessing = false } } }