0

I've parsed JSON API file into swift for nowplaying data of my radio. Now I'm able to display artist and song title in the two labels (Still looking how to fetch new data from API, when new song is playing - now the app shows static artist and title, so I need to reopen the app to see update). What I'm trying to do is to display image from artURL into UIMageView with aspect to fill mode. Here is my code:

import UIKit
import AVKit
import MediaPlayer

class ViewController: UIViewController, AVAudioPlayerDelegate {
    
    var player : AVPlayer!
    var dict = NSDictionary()
    var isPlaying = false
    let playImage = UIImage(named: "play.png")
    let pauseImage = UIImage(named: "pause.png")
    
    @IBOutlet weak var artist: UILabel!
    @IBOutlet weak var songtitle: UILabel!
    
    .......
    
    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            overrideUserInterfaceStyle = .light
            setupRemoteCommandCenter()
            //Radio API endpoint
            let urlString = "https://radioapp.com/api/nowplaying/radioapp"
            let url = URL(string: urlString)!
            let session = URLSession.shared
            let dataTask = session.dataTask(with: url) { data, response, error in
                if let error = error {
                    print(error)
                    return
                }
                guard let data = data else {
                    print("data is nil")
                    return
                }
                let decoder = JSONDecoder()
                do {
                    let radio = try decoder.decode(RadioAPI.self, from: data)
                    print(radio)
                    DispatchQueue.main.async {
                        self.songtitle.text = radio.nowPlaying.song.title
                        self.artist.text = radio.nowPlaying.song.artist
                        if let artUrl = URL(string: radio.nowPlaying.song.art) {
                         //Here I need to load an image from `artUrl`
                        }
                    }
                } catch {
                    print("Error Parsing JSON: \(error)")
                }
            }
            dataTask.resume()
        }

EDIT: My only idea is to add this, but still it does not work:

if let artUrl = URL(string: radio.nowPlaying.song.art) {
                            //I need to load an image from `artUrl`
                            let albumArt = UIImage(data: data)
                            let albumArtView = UIImageView(image: albumArt)
                        }
LewTrocki
  • 41
  • 7
  • 1
    You can use again a `URLSession` to download the image (it will be in `data`, then, it's just `UIImage(data: data)`, or if you want to put it into a `UIImageView`, there are third party lib like `Alamofire+Image`, `KingFisher`, `SDWebImage` that have convenience method for `UIImageView`: `myUIImageView.setImage(url: artUrl)`. – Larme Sep 16 '21 at 13:01
  • 1
    Unrelated, but avoid NSStuff when Stuff is available: `NSDictionary` -> `Dictionary` specifying what's the key/values classes. – Larme Sep 16 '21 at 13:02
  • @Larme Thanks, so with URLSession I can download the image to data inside my app, but than, I can't put it inside UIImageView? – LewTrocki Sep 16 '21 at 18:06
  • @Larme I've added edit in post. Is that a good way to solve this question? – LewTrocki Sep 17 '21 at 07:54
  • 1
    No, as you seen previously `data` contains the JSON. You need to call another `URLSession.datTask(...)` base one `artUrl` instead of `"https://admin.radiopromi...` – Larme Sep 17 '21 at 08:31
  • @Larme Can you write the begging for me? Do i start with ```let session = URLSession.shared```? I've mixed up sth here – LewTrocki Sep 17 '21 at 10:09
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237204/discussion-between-larme-and-lewtrocki). – Larme Sep 17 '21 at 10:10

0 Answers0