damus

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

commit 1214f1839d78d2d96e7be32532e294570f9682d9
parent 7f6540b0c08aa6fa72510063bba4f5dbd12848ca
Author: kernelkind <kernelkind@gmail.com>
Date:   Tue, 20 Feb 2024 18:48:18 -0500

media: fix gif upload regression

Uploaded gifs now upload as gifs again instead of still images.

Fixes: 904ae6c24d8eef99d1ad367cd486fc8c23c4cd7d ("Fix gif upload as still images")
Lightning-address: kernelkind@getalby.com
Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Link: 20240220234818.27472-1-kernelkind@gmail.com
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Util/Images/ImageMetadata.swift | 23+++++++++++++++++++++--
Mdamus/Views/MediaPicker.swift | 47+++++++++++++++++++++++++++++++++++++++--------
2 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/damus/Util/Images/ImageMetadata.swift b/damus/Util/Images/ImageMetadata.swift @@ -211,21 +211,40 @@ func process_image_metadatas(cache: EventCache, ev: NostrEvent) { } } +func canGetSourceTypeFromUrl(url: URL) -> Bool { + guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else { + print("Failed to create image source.") + return false + } + return CGImageSourceGetType(source) != nil +} + func removeGPSDataFromImage(fromImageURL imageURL: URL) -> Bool { guard let source = CGImageSourceCreateWithURL(imageURL as CFURL, nil) else { print("Failed to create image source.") return false } let data = NSMutableData() + + let totalCount = CGImageSourceGetCount(source) + + guard totalCount > 0 else { + print("No images found.") + return false + } + guard let type = CGImageSourceGetType(source), - let destination = CGImageDestinationCreateWithData(data, type, 1, nil) else { + let destination = CGImageDestinationCreateWithData(data, type, totalCount, nil) else { print("Failed to create image destination.") return false } let removeGPSProperties: CFDictionary = [kCGImagePropertyGPSDictionary as String: kCFNull] as CFDictionary - CGImageDestinationAddImageFromSource(destination, source, 0, removeGPSProperties) + for i in 0...totalCount { + CGImageDestinationAddImageFromSource(destination, source, i, removeGPSProperties) + } + if CGImageDestinationFinalize(destination) { do { try data.write(to: imageURL, options: .atomic) diff --git a/damus/Views/MediaPicker.swift b/damus/Views/MediaPicker.swift @@ -32,14 +32,24 @@ struct MediaPicker: UIViewControllerRepresentable { for result in results { if result.itemProvider.hasItemConformingToTypeIdentifier(UTType.image.identifier) { - result.itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in - guard let image = image as? UIImage, error == nil else { return } - let fixedImage = image.fixOrientation() + result.itemProvider.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { (item, error) in + guard let url = item as? URL else { return } - if let savedURL = self.saveImageToTemporaryFolder(image: fixedImage), - removeGPSDataFromImage(fromImageURL: savedURL) { - self.parent.onMediaPicked(.image(savedURL)) - self.parent.image_upload_confirm = true + if canGetSourceTypeFromUrl(url: url) { + // Media was not taken from camera + if let savedURL = self.saveImageToTemporaryFolder(from: url) { + self.chooseImage(url: savedURL) + } + } else { + // Media was taken from camera + result.itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in + guard let image = image as? UIImage, error == nil else { return } + let fixedImage = image.fixOrientation() + + if let savedURL = self.saveImageToTemporaryFolder(image: fixedImage) { + self.chooseImage(url: savedURL) + } + } } } } else if result.itemProvider.hasItemConformingToTypeIdentifier(UTType.movie.identifier) { @@ -53,6 +63,23 @@ struct MediaPicker: UIViewControllerRepresentable { } } } + + func chooseImage(url: URL) { + if removeGPSDataFromImage(fromImageURL: url) { + self.parent.onMediaPicked(.image(url)) + self.parent.image_upload_confirm = true + } + } + + func saveImageToTemporaryFolder(from imageUrl: URL) -> URL? { + let fileExtension = imageUrl.pathExtension + guard let imageData = try? Data(contentsOf: imageUrl) else { + print("Failed to load image data from URL.") + return nil + } + + return saveImageToTemporaryFolder(imageData: imageData, imageType: fileExtension) + } func saveImageToTemporaryFolder(image: UIImage, imageType: String = "png") -> URL? { // Convert UIImage to Data @@ -68,6 +95,10 @@ struct MediaPicker: UIViewControllerRepresentable { return nil } + return saveImageToTemporaryFolder(imageData: data, imageType: imageType) + } + + private func saveImageToTemporaryFolder(imageData: Data, imageType: String) -> URL? { // Generate a temporary URL with a unique filename let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let uniqueImageName = "\(UUID().uuidString).\(imageType)" @@ -75,7 +106,7 @@ struct MediaPicker: UIViewControllerRepresentable { // Save the image data to the temporary URL do { - try data.write(to: temporaryImageURL) + try imageData.write(to: temporaryImageURL) return temporaryImageURL } catch { print("Error saving image data to temporary URL: \(error.localizedDescription)")