I am currently working on a CarPlay project where I need to display images using CPGridButton. Initially, I was using Base64 strings to load images, but I want to switch to loading images directly from URLs for better performance.
I've implemented a method to load images asynchronously from a URL, but I'm having trouble integrating it into my existing code that uses CPGridButton. I'm unsure how to replace the Base64 image loading with URL loading effectively.
Could anyone provide guidance or examples on how to achieve this for a CarPlay interface? Any help would be greatly appreciated!"
private func updateGridTemplate() {
guard let interfaceController = self.interfaceController else { return }
let items: [CPGridButton] = storedNotifications.flatMap { notification in
notification.incidenceConfigurations?.compactMap { config in
let image: UIImage
if let base64String = config.icon, let img = Util.imageFromBase642(base64String) {
image = img
} else {
image = UIImage(named: "Icon") ?? UIImage()
}
return CPGridButton(titleVariants: [config.phrase ?? "Title"], image: image, handler: { _ in
self.showOptions(item: config)
})
} ?? []
}
let leadingButton = CPBarButton(type: .text) { barButton in
self.viewModel.makePhoneCall()
}
leadingButton.title = "CALL ME"
if trailingButton == nil {
trailingButton = CPBarButton(type: .text) { barbutton in
print("")
}
}
updateTrailingButtonTitle()
let gridTemplate = CPGridTemplate(title: "MY APP", gridButtons: items)
gridTemplate.trailingNavigationBarButtons = [trailingButton!]
gridTemplate.leadingNavigationBarButtons = [leadingButton]
interfaceController.setRootTemplate(gridTemplate, animated: true)
}
updateGridTemplate
. In theflatMap
you would fetch the UIImage from wherever you stored it. There is no way to replace the image on aCPGridButton
UIImage(named:)