From 77a6654ff20df8d3f946dd5a2cbf3101d36f7f4d Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 29 Oct 2022 11:47:07 -0400 Subject: [PATCH] Fix crash when generating blurhash image for AttachmentView that hasn't been laid out It was passing a negative size into the blurhash decoder, which is invalid Instead, cap the size at 32x32 (letting the image view scale it up when rendering) --- Tusker/Views/Attachments/AttachmentView.swift | 37 ++++++++++++------- .../AttachmentsContainerView.swift | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 47a64824..f256ad8c 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -26,7 +26,6 @@ class AttachmentView: GIFImageView { var attachment: Attachment! var index: Int! - var expectedSize: CGSize! private var attachmentRequest: ImageCache.Request? private var source: Source? @@ -37,13 +36,12 @@ class AttachmentView: GIFImageView { private var isGrayscale = false - init(attachment: Attachment, index: Int, expectedSize: CGSize) { + init(attachment: Attachment, index: Int) { super.init(image: nil) commonInit() self.attachment = attachment self.index = index - self.expectedSize = expectedSize loadAttachment() } @@ -112,18 +110,8 @@ class AttachmentView: GIFImageView { if let hash = attachment.blurHash { AttachmentView.queue.async { [weak self] in guard let self = self else { return } - let size: CGSize - if let meta = self.attachment.meta, - let width = meta.width, let height = meta.height { - size = CGSize(width: width, height: height) - } else if let orig = self.attachment.meta?.original, - let width = orig.width, let height = orig.height { - size = CGSize(width: width, height: height) - } else { - size = self.expectedSize - } - guard var preview = UIImage(blurHash: hash, size: size) else { + guard var preview = UIImage(blurHash: hash, size: self.blurHashSize()) else { return } @@ -153,6 +141,27 @@ class AttachmentView: GIFImageView { } } + private func blurHashSize() -> CGSize { + if let meta = self.attachment.meta { + let aspectRatio: CGFloat + if let width = meta.width, let height = meta.height { + aspectRatio = CGFloat(width) / CGFloat(height) + } else if let orig = meta.original, + let width = orig.width, let height = orig.height { + aspectRatio = CGFloat(width) / CGFloat(height) + } else { + return CGSize(width: 32, height: 32) + } + if aspectRatio > 1 { + return CGSize(width: 32, height: 32 / aspectRatio) + } else { + return CGSize(width: 32 * aspectRatio, height: 32) + } + } else { + return CGSize(width: 32, height: 32) + } + } + func loadImage() { let attachmentURL = attachment.url attachmentRequest = ImageCache.attachments.get(attachmentURL) { [weak self] (data, _) in diff --git a/Tusker/Views/Attachments/AttachmentsContainerView.swift b/Tusker/Views/Attachments/AttachmentsContainerView.swift index ae84c170..aada5c97 100644 --- a/Tusker/Views/Attachments/AttachmentsContainerView.swift +++ b/Tusker/Views/Attachments/AttachmentsContainerView.swift @@ -257,7 +257,7 @@ class AttachmentsContainerView: UIView { } let size = CGSize(width: width, height: height) - let attachmentView = AttachmentView(attachment: attachments[index], index: index, expectedSize: size) + let attachmentView = AttachmentView(attachment: attachments[index], index: index) attachmentView.delegate = delegate attachmentView.translatesAutoresizingMaskIntoConstraints = false attachmentView.accessibilityLabel = String(format: NSLocalizedString("Attachment %d", comment: "attachment at index accessiblity label"), index + 1)