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)
This commit is contained in:
parent
43aee0ec67
commit
77a6654ff2
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue