From e4959ff156d9bee7c3378e6d528911abc58d67b7 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:52:23 +0200 Subject: [PATCH] Fix for flipped image notification placeholder on iPhone (#1200) * fix * better handling * reverted wrong commit * typo in changlog --- .../Extensions/UNNotificationContent.swift | 29 +++++++++++++++++-- changelog.d/1194.bugfix | 1 + 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 changelog.d/1194.bugfix diff --git a/ElementX/Sources/Other/Extensions/UNNotificationContent.swift b/ElementX/Sources/Other/Extensions/UNNotificationContent.swift index 3d9adbf10..c27450e89 100644 --- a/ElementX/Sources/Other/Extensions/UNNotificationContent.swift +++ b/ElementX/Sources/Other/Extensions/UNNotificationContent.swift @@ -122,7 +122,7 @@ extension UNMutableNotificationContent { if let fetchedImage { image = fetchedImage } else if let data = await getPlaceholderAvatarImageData(name: icon.groupInfo?.name ?? senderName, - id: icon.groupInfo?.name ?? senderName) { + id: icon.groupInfo?.id ?? senderID) { image = INImage(imageData: data) } else { image = INImage(named: "") @@ -181,6 +181,31 @@ extension UNMutableNotificationContent { .clipShape(Circle()) .frame(width: 100, height: 100) let renderer = await ImageRenderer(content: image) - return await renderer.uiImage?.jpegData(compressionQuality: 0.8) + guard let image = await renderer.uiImage else { + return nil + } + + // On simulator and macOS the image is rendered correctly + // But on other devices is rendered upside down so we need to flip it + #if targetEnvironment(simulator) + return image.pngData() + #else + if ProcessInfo.processInfo.isiOSAppOnMac { + return image.pngData() + } else { + return image.flippedVertically().pngData() + } + #endif + } +} + +private extension UIImage { + func flippedVertically() -> UIImage { + let format = UIGraphicsImageRendererFormat() + format.scale = scale + return UIGraphicsImageRenderer(size: size, format: format).image { context in + context.cgContext.concatenate(CGAffineTransform(scaleX: 1, y: -1)) + self.draw(at: CGPoint(x: 0, y: -size.height)) + } } } diff --git a/changelog.d/1194.bugfix b/changelog.d/1194.bugfix new file mode 100644 index 000000000..22d3895c5 --- /dev/null +++ b/changelog.d/1194.bugfix @@ -0,0 +1 @@ +Fix for the flipped notification image placeholder on iOS. \ No newline at end of file