Skip to content

Commit

Permalink
Merge pull request #1239 from planetary-social/bugfix/take-photo-video
Browse files Browse the repository at this point in the history
Fix issue where taking photo from app does nothing
  • Loading branch information
joshuatbrown authored Jun 17, 2024
2 parents 9ced9cc + f1b34b1 commit 915db17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Added our third cohort of creators and journalists to the Discover tab.
- Fixed a bug where taking a photo in the app didn’t work.

## [0.1.17] - 2024-06-10Z

Expand Down
24 changes: 23 additions & 1 deletion Nos/Views/New Note/ImagePickerUIViewController.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger
import SwiftUI
import UniformTypeIdentifiers

Expand Down Expand Up @@ -30,7 +31,6 @@ struct ImagePickerUIViewController: UIViewControllerRepresentable {
}

final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var onCompletion: ((URL?) -> Void)

init(onCompletion: @escaping ((URL?) -> Void)) {
Expand All @@ -40,6 +40,7 @@ struct ImagePickerUIViewController: UIViewControllerRepresentable {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
onCompletion(nil)
}

func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
Expand All @@ -48,9 +49,30 @@ struct ImagePickerUIViewController: UIViewControllerRepresentable {
onCompletion(videoURL)
} else if let imageURL = info[.imageURL] as? URL {
onCompletion(imageURL)
} else if let image = info[.originalImage] as? UIImage,
let imageData = image.jpegData(compressionQuality: 1.0) {
let url = saveImage(imageData)
onCompletion(url)
} else {
onCompletion(nil)
}
}

/// Saves the given image data to a JPG file and returns the URL of the file.
/// - Parameter imageData: The image data to save to disk.
/// - Returns: The URL of the image file.
private func saveImage(_ imageData: Data) -> URL? {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = urls[0]
let fileURL = documentDirectory.appendingPathComponent("capturedImage.jpg")

do {
try imageData.write(to: fileURL, options: .atomic)
return fileURL
} catch {
Log.debug("Error saving image: \(error)")
return nil
}
}
}
}

0 comments on commit 915db17

Please sign in to comment.