Sunday, April 19, 2026
HomeiOS Developmentios - cannot load random picture from API

ios – cannot load random picture from API

[ad_1]

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imageOfDog: UIImageView!

struct dataForLoading: Codable {
    let message: String
}
override func viewDidLoad() {
    tremendous.viewDidLoad()
    
    // load url
    
    let url = "https://canine.ceo/api/breeds/picture/random"
    guard let loadUrl = URL(string: url) else { return }
 
    // use loaded url in urlSession
    
    URLSession.shared.dataTask(with: loadUrl) {(information, response, error) in
        if error != nil{
            print("if error printed")
            print(error!.localizedDescription)
        }
        
        // decode
        
        guard let information = information else { return }
        do {
            let jsonData = strive JSONDecoder().decode(dataForLoading.self, from: information)
          
            DispatchQueue.most important.async {
                self.imageOfDog.picture = UIImage(named: jsonData.message)
             
            }
        }
        catch let jsonError {
            print(jsonError)
        }
        
    }.resume()
}
 

}

i’m currentlt utilizing. https://canine.ceo/api/breeds/picture/random. this api

for loading random picture

i’m new to loading Api i’m attempting to load API by URLSession

when i run venture i get under error

Random canine picture[5960:196973] [framework] CUIThemeStore: No theme registered with id=0

i believe i’m not capable of decode it correctly how am i able to load picture by API

At First Api Generates an url from picture like these. {“message”:”https://photos.canine.ceo/breeds/elkhound-norwegian/n02091467_5985.jpg”,”standing”:”success”}

so my thought is to get first API and in Api whaterver url is coming cross it to imageview

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments