damus

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

commit 7a55ea13e3c548221a2dd11145fa060d16b43ab9
parent 2815827b9f29ecb6e78af0043ad663c397ef622e
Author: Joel Klabo <joelklabo@gmail.com>
Date:   Fri, 31 Mar 2023 13:45:41 -0700

Save Camera Images Locally Then Upload

Closes: #847

Diffstat:
Mdamus/Views/AttachMediaUtility.swift | 43++++++++++++++++++++++++++++++++++++++++---
1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/damus/Views/AttachMediaUtility.swift b/damus/Views/AttachMediaUtility.swift @@ -40,9 +40,9 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") switch mediaToUpload { - case .image(let img): + case .image(let url): do { - mediaData = try Data(contentsOf: img) + mediaData = try Data(contentsOf: url) } catch { return .failed(error) } @@ -113,7 +113,15 @@ extension PostView { 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() } @@ -121,6 +129,35 @@ extension PostView { 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 {