I understand how to utilize DispatchQueue with UIKit, but not with SwiftUI, because HorizonCalendar requires SwiftUI to function. Everything works perfectly, however as I browse down and load an image, the scrolling lags. I attempted to add DispatchQueue, however the SwiftUI rejected it. How can I include DispatchQueue in this code so that it may load images while scrolling?
Main ViewController
import UIKit
import SwiftUI
import HorizonCalendar
class TheGalleryView: UIViewController {
override func viewWillAppear(_ animated: Bool) {
let textAttributes = [NSAttributedString.Key.foregroundColor: colorTint]
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.largeTitleTextAttributes = textAttributes
navigationItem.leftBarButtonItem?.tintColor = colorTint
}
override func viewDidLoad() {
super.viewDidLoad()
title = getString(rawString: "Gallery")
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "gear"), style: .plain, target: self, action: #selector(openSetting))
view.backgroundColor = UIColor(named: "Main Background")
createCalendar()
}
func createCalendar() {
let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2023))!
let endDate = calendar.date(from: DateComponents(year: 2023, month: 12, day: 31))!
let content = CalendarViewContent(calendar: calendar,
visibleDateRange: startDate...endDate,
monthsLayout: .vertical(options: VerticalMonthsLayoutOptions()))
.interMonthSpacing(25)
.dayItemProvider { day in
ZStack {
if getFirstPhotoFromGallery(fromDay: day.components) != nil {
Image(uiImage: getFirstPhotoFromGallery(fromDay: day.components)!)
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(7)
.opacity(0.4)
}
Text("\(day.day)")
.font(.system(size: 17, weight: .semibold))
.foregroundColor(Color(UIColor(named: "Font Color")!))
}
.calendarItemModel
}
.interMonthSpacing(24)
.verticalDayMargin(8)
.horizontalDayMargin(8)
let calendarView = CalendarView(initialContent: content)
calendarView.daySelectionHandler = { day in
// MARK: The date has been selected
}
calendarView.translatesAutoresizingMaskIntoConstraints = false
calendarView.backgroundColor = UIColor(named: "Main Background")
view.addSubview(calendarView)
calendarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
calendarView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
calendarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
calendarView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
}
}
Extension code, to load the image from data core
func getFirstPhotoFromGallery(fromDay: DateComponents) -> UIImage? {
var prePhoto = UIImage(systemName: "")?.withTintColor(.clear)
let freshDate = Calendar.current.date(from: fromDay)
let getDailyData = theDailyData.first(where: { $0.date == freshDate })
if getDailyData != nil {
let searchTheImage = thePhotoData.first(where: { $0.belongTo == getDailyData?.theID })
if searchTheImage != nil {
let getPhoto = UIImage(data: (searchTheImage?.thePhotoData)!)?.jpegData(compressionQuality: 0.25)
prePhoto = UIImage(data: getPhoto!)
// This doesn't accepted the return inside the DispatchQueue
}
}
return prePhoto
}