diff --git a/CHANGELOG.md b/CHANGELOG.md index 738922fd3..b0e9d7bec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). # Upcoming ### 🔄 Changed +- Uploading a HEIC photo from the library is now converted to JPEG for better compatibility [#767](https://github.com/GetStream/stream-chat-swiftui/pull/767) # [4.73.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.73.0) _February 28, 2025_ diff --git a/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift b/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift index 0d531b8ce..f20adb592 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift @@ -55,7 +55,7 @@ final class ImagePickerCoordinator: NSObject, UIImagePickerControllerDelegate, U didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any] ) { if let uiImage = info[.originalImage] as? UIImage, - let imageURL = try? uiImage.temporaryLocalFileUrl() { + let imageURL = try? uiImage.saveAsJpgToTemporaryUrl() { let addedImage = AddedAsset( image: uiImage, id: UUID().uuidString, diff --git a/Sources/StreamChatSwiftUI/ChatChannel/Composer/PhotoAttachmentPickerView.swift b/Sources/StreamChatSwiftUI/ChatChannel/Composer/PhotoAttachmentPickerView.swift index 79a9665a2..af4bec469 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/Composer/PhotoAttachmentPickerView.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/Composer/PhotoAttachmentPickerView.swift @@ -102,7 +102,7 @@ public struct PhotoAttachmentCell: View { .allowsHitTesting(true) .onTapGesture { withAnimation { - if let assetURL = assetURL { + if let assetURL = assetJpgURL() { onImageTap( AddedAsset( image: image, @@ -193,4 +193,13 @@ public struct PhotoAttachmentCell: View { } } } + + /// The original photo is usually in HEIC format. + /// This makes sure that the photo is converted to JPG. + /// This way it is more compatible with other platforms. + private func assetJpgURL() -> URL? { + guard let assetURL = assetURL else { return nil } + guard let assetData = try? Data(contentsOf: assetURL) else { return nil } + return try? UIImage(data: assetData)?.saveAsJpgToTemporaryUrl() + } } diff --git a/Sources/StreamChatSwiftUI/Utils/Common/UIImage+Extensions.swift b/Sources/StreamChatSwiftUI/Utils/Common/UIImage+Extensions.swift index 8e7f7c9c6..30a0eb2ef 100644 --- a/Sources/StreamChatSwiftUI/Utils/Common/UIImage+Extensions.swift +++ b/Sources/StreamChatSwiftUI/Utils/Common/UIImage+Extensions.swift @@ -42,7 +42,7 @@ extension UIImage { } extension UIImage { - func temporaryLocalFileUrl() throws -> URL? { + func saveAsJpgToTemporaryUrl() throws -> URL? { guard let imageData = jpegData(compressionQuality: 1.0) else { return nil } let imageName = "\(UUID().uuidString).jpg" let documentDirectory = NSTemporaryDirectory()