damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 88740a2bc70ed73c158729ecdf0485d4faad2722
parent e3f578e48a99cd0529d7b8115c9517aaf767f0db
Author: Joel Klabo <joelklabo@gmail.com>
Date:   Mon,  3 Apr 2023 16:09:33 -0700

Fix Camera Uploads Again

Changelog-Fixed: Fix broken camera uploads (again)

Diffstat:
Mdamus/Views/ImagePicker.swift | 45+++++++++++++++++++++++++++++++++++++++++----
1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/damus/Views/ImagePicker.swift b/damus/Views/ImagePicker.swift @@ -34,23 +34,60 @@ struct ImagePicker: UIViewControllerRepresentable { self.onImagePicked = onImagePicked self.onVideoPicked = onVideoPicked } - + func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL { // Handle the selected video onVideoPicked(videoURL) } else if let imageURL = info[UIImagePickerController.InfoKey.imageURL] as? URL { // Handle the selected image - self.onImagePicked(imageURL) + onImagePicked(imageURL) + } else if let cameraImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { + if let imageURL = saveImageToTemporaryFolder(image: cameraImage, imageType: "jpeg") { + onImagePicked(imageURL) + } + } else if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage { + if let editedImageURL = saveImageToTemporaryFolder(image: editedImage) { + onImagePicked(editedImageURL) + } } presentationMode.dismiss() } - + func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { presentationMode.dismiss() } + + func saveImageToTemporaryFolder(image: UIImage, imageType: String = "png") -> URL? { + // Convert UIImage to Data + let imageData: Data? + if imageType.lowercased() == "jpeg" { + imageData = image.jpegData(compressionQuality: 1.0) + } else { + imageData = image.pngData() + } + + guard let data = imageData else { + print("Failed to convert UIImage to Data.") + return nil + } + + // Generate a temporary URL with a unique filename + let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) + let uniqueImageName = "\(UUID().uuidString).\(imageType)" + let temporaryImageURL = temporaryDirectoryURL.appendingPathComponent(uniqueImageName) + + // Save the image data to the temporary URL + do { + try data.write(to: temporaryImageURL) + return temporaryImageURL + } catch { + print("Error saving image data to temporary URL: \(error.localizedDescription)") + return nil + } + } } - + func makeCoordinator() -> Coordinator { return Coordinator(presentationMode: presentationMode, sourceType: sourceType,