Grayscale images in new gallery

See #450
This commit is contained in:
Shadowfacts 2024-03-20 11:54:35 -04:00
parent a80cbe79c2
commit d36e0ad27d
1 changed files with 28 additions and 1 deletions

View File

@ -19,12 +19,16 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
let image: UIImage let image: UIImage
let gifController: GIFController? let gifController: GIFController?
private var imageView: GIFImageView!
@available(iOS 16.0, macCatalyst 17.0, *) @available(iOS 16.0, macCatalyst 17.0, *)
private static let analyzer = ImageAnalyzer() private static let analyzer = ImageAnalyzer()
private var _analysisInteraction: AnyObject? private var _analysisInteraction: AnyObject?
@available(iOS 16.0, macCatalyst 17.0, *) @available(iOS 16.0, macCatalyst 17.0, *)
private var analysisInteraction: ImageAnalysisInteraction? { _analysisInteraction as? ImageAnalysisInteraction } private var analysisInteraction: ImageAnalysisInteraction? { _analysisInteraction as? ImageAnalysisInteraction }
private var isGrayscale = false
init(url: URL, caption: String?, originalData: Data?, image: UIImage, gifController: GIFController?) { init(url: URL, caption: String?, originalData: Data?, image: UIImage, gifController: GIFController?) {
self.url = url self.url = url
self.caption = caption self.caption = caption
@ -44,7 +48,14 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
let imageView = GIFImageView(image: image) isGrayscale = Preferences.shared.grayscaleImages
let maybeGrayscaleImage = if isGrayscale {
ImageGrayscalifier.convert(url: url, image: image) ?? image
} else {
image
}
imageView = GIFImageView(image: maybeGrayscaleImage)
imageView.translatesAutoresizingMaskIntoConstraints = false imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit imageView.contentMode = .scaleAspectFit
imageView.isUserInteractionEnabled = true imageView.isUserInteractionEnabled = true
@ -75,6 +86,8 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
} }
} }
} }
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
} }
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
@ -85,6 +98,20 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
} }
} }
@objc private func preferencesChanged() {
if isGrayscale != Preferences.shared.grayscaleImages {
isGrayscale = Preferences.shared.grayscaleImages
let image = if isGrayscale {
ImageGrayscalifier.convert(url: url, image: image)
} else {
image
}
if let image {
imageView.image = image
}
}
}
// MARK: GalleryContentViewController // MARK: GalleryContentViewController
var container: (any GalleryVC.GalleryContentViewControllerContainer)? var container: (any GalleryVC.GalleryContentViewControllerContainer)?