Add BlurHash

This commit is contained in:
Justin Mazzocchi 2021-03-03 17:15:40 -08:00
parent 187bc42373
commit 19963de055
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
8 changed files with 83 additions and 12 deletions

View File

@ -37,9 +37,9 @@ public struct Attachment: Codable, Hashable {
public let url: URL
public let remoteUrl: URL?
public let previewUrl: URL?
public let textUrl: URL?
public let meta: Meta?
public let description: String?
public let blurhash: String?
}
public extension Attachment {

View File

@ -91,6 +91,7 @@
D0625E59250F092900502611 /* StatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0625E58250F092900502611 /* StatusTableViewCell.swift */; };
D0625E5D250F0B5C00502611 /* StatusContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0625E5C250F0B5C00502611 /* StatusContentConfiguration.swift */; };
D06BC5E625202AD90079541D /* ProfileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06BC5E525202AD90079541D /* ProfileViewController.swift */; };
D06FA32725F0659600C9639A /* BlurHash in Frameworks */ = {isa = PBXBuildFile; productRef = D06FA32625F0659600C9639A /* BlurHash */; };
D07EC7CF25B13921006DF726 /* PickerEmoji+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07EC7CE25B13921006DF726 /* PickerEmoji+Extensions.swift */; };
D07EC7D025B13921006DF726 /* PickerEmoji+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07EC7CE25B13921006DF726 /* PickerEmoji+Extensions.swift */; };
D07EC7DC25B13DBB006DF726 /* EmojiCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07EC7DB25B13DBB006DF726 /* EmojiCollectionViewCell.swift */; };
@ -183,6 +184,7 @@
D0D2AC4D25BCD2A9003D5DF2 /* TagTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D2AC4C25BCD2A9003D5DF2 /* TagTableViewCell.swift */; };
D0D2AC5325BCD2BA003D5DF2 /* TagContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D2AC5225BCD2BA003D5DF2 /* TagContentConfiguration.swift */; };
D0D2AC6725BD0484003D5DF2 /* LineChartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D2AC6625BD0484003D5DF2 /* LineChartView.swift */; };
D0D4301425F067D600BE5504 /* BlurHash in Frameworks */ = {isa = PBXBuildFile; productRef = D0D4301325F067D600BE5504 /* BlurHash */; };
D0D93EBA25D9C70400C622ED /* AutocompleteItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D93EB925D9C70400C622ED /* AutocompleteItemView.swift */; };
D0D93EC025D9C71D00C622ED /* AutocompleteItemContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D93EBF25D9C71D00C622ED /* AutocompleteItemContentConfiguration.swift */; };
D0D93EC525D9C75E00C622ED /* AutocompleteItemContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D93EBF25D9C71D00C622ED /* AutocompleteItemContentConfiguration.swift */; };
@ -442,6 +444,7 @@
buildActionMask = 2147483647;
files = (
D0E2C1D124FD97F000854680 /* ViewModels in Frameworks */,
D06FA32725F0659600C9639A /* BlurHash in Frameworks */,
D04F34B625E42ABE00714251 /* SDWebImage in Frameworks */,
D0FE7C8025C4C79F00203957 /* PreviewViewModels in Frameworks */,
);
@ -458,6 +461,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D0D4301425F067D600BE5504 /* BlurHash in Frameworks */,
D04F34BC25E42ADC00714251 /* SDWebImage in Frameworks */,
D04F9E8E259E9C950081B0C9 /* ViewModels in Frameworks */,
);
@ -862,6 +866,7 @@
D0E2C1D024FD97F000854680 /* ViewModels */,
D0FE7C7F25C4C79F00203957 /* PreviewViewModels */,
D04F34B525E42ABE00714251 /* SDWebImage */,
D06FA32625F0659600C9639A /* BlurHash */,
);
productName = "Metatext (iOS)";
productReference = D047FA8C24C3E21200AF17C5 /* Metatext.app */;
@ -903,6 +908,7 @@
packageProductDependencies = (
D04F9E8D259E9C950081B0C9 /* ViewModels */,
D04F34BB25E42ADC00714251 /* SDWebImage */,
D0D4301325F067D600BE5504 /* BlurHash */,
);
productName = "Share Extension";
productReference = D08E526C257C36CA00FA2C5F /* Share Extension.appex */;
@ -966,6 +972,7 @@
mainGroup = D047FA7F24C3E21000AF17C5;
packageReferences = (
D04F34B425E42ABE00714251 /* XCRemoteSwiftPackageReference "SDWebImage" */,
D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */,
);
productRefGroup = D047FA8D24C3E21200AF17C5 /* Products */;
projectDirPath = "";
@ -1664,6 +1671,14 @@
minimumVersion = 5.10.4;
};
};
D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/metabolist/blurhash.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
@ -1690,6 +1705,16 @@
isa = XCSwiftPackageProductDependency;
productName = ViewModels;
};
D06FA32625F0659600C9639A /* BlurHash */ = {
isa = XCSwiftPackageProductDependency;
package = D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */;
productName = BlurHash;
};
D0D4301325F067D600BE5504 /* BlurHash */ = {
isa = XCSwiftPackageProductDependency;
package = D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */;
productName = BlurHash;
};
D0E2C1D024FD97F000854680 /* ViewModels */ = {
isa = XCSwiftPackageProductDependency;
productName = ViewModels;

View File

@ -10,6 +10,15 @@
"version": "1.0.0"
}
},
{
"package": "BlurHash",
"repositoryURL": "https://github.com/metabolist/blurhash.git",
"state": {
"branch": null,
"revision": "4d86c470d866b2d8989adc5a65b121221e40df54",
"version": "1.0.0"
}
},
{
"package": "CodableBloomFilter",
"repositoryURL": "https://github.com/metabolist/codable-bloom-filter.git",

View File

@ -1,6 +1,7 @@
// Copyright © 2020 Metabolist. All rights reserved.
import AVFoundation
import BlurHash
import Mastodon
import SDWebImage
import UIKit
@ -111,11 +112,16 @@ final class ImageViewController: UIViewController {
imageView.tag = viewModel.tag
playerView.isHidden = true
let placeholderKey = viewModel.attachment.previewUrl?.absoluteString
let placeholderImage = SDImageCache.shared.imageFromCache(forKey: placeholderKey)
let placeholderImage: UIImage?
let cachedImageKey = viewModel.attachment.previewUrl?.absoluteString
let cachedImage = SDImageCache.shared.imageFromCache(forKey: cachedImageKey)
if placeholderImage != nil {
imageView.sd_imageIndicator = nil
if cachedImage != nil {
placeholderImage = cachedImage
} else if let blurHash = viewModel.attachment.blurhash {
placeholderImage = UIImage(blurHash: blurHash, size: .blurHashSize)
} else {
placeholderImage = nil
}
imageView.sd_setImage(with: viewModel.attachment.url, placeholderImage: placeholderImage)

View File

@ -22,6 +22,7 @@ struct AcknowledgmentsView: View {
private extension AcknowledgmentsView {
enum Acknowledgment: CaseIterable {
case blurHash
case combineExpectations
case grdb
case sdwebimage
@ -32,6 +33,8 @@ private extension AcknowledgmentsView {
extension AcknowledgmentsView.Acknowledgment {
var name: String {
switch self {
case .blurHash:
return "BlurHash"
case .combineExpectations:
return "CombineExpectations"
case .grdb:
@ -45,6 +48,8 @@ extension AcknowledgmentsView.Acknowledgment {
var url: URL {
switch self {
case .blurHash:
return URL(string: "https://github.com/woltapp/blurhash")!
case .combineExpectations:
return URL(string: "https://github.com/groue/CombineExpectations")!
case .grdb:
@ -59,6 +64,16 @@ extension AcknowledgmentsView.Acknowledgment {
// swiftlint:disable line_length
var license: String {
switch self {
case .blurHash:
return """
Copyright (c) 2018 Wolt Enterprises
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
case .combineExpectations:
return """
Copyright (C) 2019 Gwendal Roué

View File

@ -1,6 +1,7 @@
// Copyright © 2020 Metabolist. All rights reserved.
import AVKit
import BlurHash
import Combine
import SDWebImage
import UIKit
@ -169,7 +170,17 @@ private extension AttachmentView {
switch viewModel.attachment.type {
case .image, .video, .gifv:
imageView.sd_setImage(with: viewModel.attachment.previewUrl) { [weak self] _, _, _, _ in
let placeholderImage: UIImage?
if let blurHash = viewModel.attachment.blurhash {
placeholderImage = UIImage(blurHash: blurHash, size: .blurHashSize)
} else {
placeholderImage = nil
}
imageView.sd_setImage(
with: viewModel.attachment.previewUrl,
placeholderImage: placeholderImage) { [weak self] _, _, _, _ in
self?.layoutSubviews()
}
case .audio:

View File

@ -1,5 +1,6 @@
// Copyright © 2021 Metabolist. All rights reserved.
import BlurHash
import Combine
import SDWebImage
import UIKit
@ -142,15 +143,15 @@ private extension EditThumbnailView {
switch viewModel.attachment.type {
case .image:
playerView.isHidden = true
let placeholderImage: UIImage?
let placeholderKey = viewModel.attachment.previewUrl?.absoluteString
let placeholderImage = SDImageCache.shared.imageFromCache(forKey: placeholderKey)
if placeholderImage != nil {
imageView.sd_imageIndicator = nil
if let blurHash = viewModel.attachment.blurhash {
placeholderImage = UIImage(blurHash: blurHash, size: .blurHashSize)
} else {
placeholderImage = nil
}
imageView.sd_setImage(with: viewModel.attachment.url, placeholderImage: placeholderImage)
imageView.sd_setImage(with: viewModel.attachment.previewUrl, placeholderImage: placeholderImage)
case .gifv:
imageView.isHidden = true
let player = PlayerCache.shared.player(url: viewModel.attachment.url)

View File

@ -21,6 +21,10 @@ extension Float {
static let defaultShadowOpacity: Self = 0.25
}
extension CGSize {
static let blurHashSize = Self(width: 32, height: 32)
}
extension CGRect {
static let defaultContentsRect = Self(origin: .zero, size: .init(width: 1, height: 1))
}