From 5cf2bc4fbf32ca9b01ed49a8c1d5beead467be95 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 1 Jun 2024 10:44:43 -0700 Subject: [PATCH] Fix profile header images being blurry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Due to the old method using ImageCache.avatars for the headers 🤦 Closes #494 --- Tusker/Views/CachedImageView.swift | 5 ++ .../Profile Header/ProfileHeaderView.swift | 60 +++---------------- .../Profile Header/ProfileHeaderView.xib | 6 +- 3 files changed, 17 insertions(+), 54 deletions(-) diff --git a/Tusker/Views/CachedImageView.swift b/Tusker/Views/CachedImageView.swift index fe23e4ff..3ca93ce8 100644 --- a/Tusker/Views/CachedImageView.swift +++ b/Tusker/Views/CachedImageView.swift @@ -33,6 +33,11 @@ class CachedImageView: UIImageView { commonInit() } + deinit { + fetchTask?.cancel() + blurHashTask?.cancel() + } + private func commonInit() { NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) } diff --git a/Tusker/Views/Profile Header/ProfileHeaderView.swift b/Tusker/Views/Profile Header/ProfileHeaderView.swift index 3628a020..a30c69da 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderView.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderView.swift @@ -25,9 +25,9 @@ class ProfileHeaderView: UIView { weak var delegate: ProfileHeaderViewDelegate? var mastodonController: MastodonController! { delegate?.apiController } - @IBOutlet weak var headerImageView: UIImageView! + @IBOutlet weak var headerImageView: CachedImageView! @IBOutlet weak var avatarContainerView: UIView! - @IBOutlet weak var avatarImageView: UIImageView! + @IBOutlet weak var avatarImageView: CachedImageView! @IBOutlet weak var moreButton: ProfileHeaderButton! @IBOutlet weak var followButton: ProfileHeaderButton! @IBOutlet weak var displayNameLabel: AccountDisplayNameLabel! @@ -44,8 +44,6 @@ class ProfileHeaderView: UIView { var accountID: String! - private var imagesTask: Task? - private var isGrayscale = false private var followButtonMode = FollowButtonMode.follow { didSet { @@ -56,10 +54,6 @@ class ProfileHeaderView: UIView { } } - deinit { - imagesTask?.cancel() - } - override func awakeFromNib() { super.awakeFromNib() @@ -69,11 +63,13 @@ class ProfileHeaderView: UIView { avatarContainerView.layer.cornerCurve = .continuous // Set zPositions so the gallery presentation/dismissal animation looks correct. avatarContainerView.layer.zPosition = 2 + avatarImageView.cache = .avatars avatarImageView.layer.masksToBounds = true avatarImageView.layer.cornerCurve = .continuous avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(avatarPressed))) avatarImageView.isUserInteractionEnabled = true + headerImageView.cache = .headers headerImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerPressed))) headerImageView.isUserInteractionEnabled = true headerImageView.layer.zPosition = 1 @@ -138,11 +134,11 @@ class ProfileHeaderView: UIView { usernameLabel.text = "@\(account.acct)" lockImageView.isHidden = !account.locked - imagesTask?.cancel() - let avatar = account.avatar - let header = account.header - imagesTask = Task { - await updateImages(avatar: avatar, header: header) + if let avatar = account.avatar { + avatarImageView.update(for: avatar) + } + if let header = account.header { + headerImageView.update(for: header) } moreButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: delegate?.actionsForProfile(accountID: accountID, source: .view(moreButton), fetchRelationship: false) ?? []) @@ -294,44 +290,6 @@ class ProfileHeaderView: UIView { avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) displayNameLabel.updateForAccountDisplayName(account: account) - - if isGrayscale != Preferences.shared.grayscaleImages { - isGrayscale = Preferences.shared.grayscaleImages - imagesTask?.cancel() - let avatar = account.avatar - let header = account.header - imagesTask = Task { - await updateImages(avatar: avatar, header: header) - } - } - } - - private nonisolated func updateImages(avatar: URL?, header: URL?) async { - await withTaskGroup(of: Void.self) { group in - group.addTask { - guard let avatar, - let image = await ImageCache.avatars.get(avatar, loadOriginal: true).1, - let transformedImage = await ImageGrayscalifier.convertIfNecessary(url: avatar, image: image), - !Task.isCancelled else { - return - } - await MainActor.run { - self.avatarImageView.image = transformedImage - } - } - group.addTask { - guard let header, - let image = await ImageCache.avatars.get(header, loadOriginal: true).1, - let transformedImage = await ImageGrayscalifier.convertIfNecessary(url: header, image: image), - !Task.isCancelled else { - return - } - await MainActor.run { - self.headerImageView.image = transformedImage - } - } - await group.waitForAll() - } } private func formatBigNumber(_ value: Int) -> (String, String) { diff --git a/Tusker/Views/Profile Header/ProfileHeaderView.xib b/Tusker/Views/Profile Header/ProfileHeaderView.xib index 086b35df..daa54c54 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderView.xib +++ b/Tusker/Views/Profile Header/ProfileHeaderView.xib @@ -3,7 +3,7 @@ - + @@ -14,7 +14,7 @@ - + @@ -23,7 +23,7 @@ - +