0

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)
    }
8
  • Where do the base 64 strings come from? Are they static? It would be better to convert them once rather than in the flatmap. You haven't shown your async mage loading code, but essentially when the image has been fetched you would need to store it somewhere was then call updateGridTemplate. In the flatMap you would fetch the UIImage from wherever you stored it. There is no way to replace the image on a CPGridButton
    – Paulw11
    Commented Sep 20, 2024 at 18:03
  • Are these image urls file URLs, or are they http URLS that point to images on the internet?
    – Duncan C
    Commented Sep 20, 2024 at 20:18
  • If these images are static and saved locally, you'd be better off saving them in your app's image catalog and loading them using UIImage(named:)
    – Duncan C
    Commented Sep 20, 2024 at 20:19
  • @DuncanC Currently, the images come from a Base64 string retrieved from an endpoint in my API. Now, I want to switch to using URLs for the icons, which will also be served via HTTP and come from the internet.
    – lacaveira
    Commented Sep 23, 2024 at 12:23
  • @Paulw11 The images are not static; they are coming from my API as Base64 strings. I’d like to switch to using URLs to display the icons instead. Im little confused about it .
    – lacaveira
    Commented Sep 23, 2024 at 12:25

0

Browse other questions tagged or ask your own question.