0
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imageOfDog: UIImageView!

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

}

i am currentlt using. https://dog.ceo/api/breeds/image/random. this api

for loading random image

i am new to loading Api i am trying to load API through URLSession

when i run project i get below error

Random dog image[5960:196973] [framework] CUIThemeStore: No theme registered with id=0

i think i am not able to decode it properly how can i load image through API

At First Api Generates an url from image like these. {"message":"https://images.dog.ceo/breeds/elkhound-norwegian/n02091467_5985.jpg","status":"success"}

so my idea is to get first API and in Api whaterver url is coming pass it to imageview

1 Answers1

-1

The error occurs cause of UIImage(named: jsonData.message) . You can call this only if the image is exist in Assets Folder. You have to use UIImage(data: data)

Example of usage

     if let imageURL = URL(string: jsonData.message){
        if let data = try? Data(contentsOf: imageURL){
             self.imageOfDog.image = UIImage(data: data)
        }
    }
Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
  • if let data = try? Data(contentsOf: imageURL) what this line conveys ? – heli bhadeshiya Aug 16 '22 at 10:56
  • Its converts string to data that can throw error. You have to get Data object for showing image from url – Omer Tekbiyik Aug 16 '22 at 10:59
  • is it just similar to try catch block ? – heli bhadeshiya Aug 16 '22 at 11:01
  • Not similar , same – Omer Tekbiyik Aug 16 '22 at 11:02
  • You should never be using `Data(contentsOf:)` for loading data from remote URLs, since that's a blocking method. Especially not on the main thread. The correct solution is [indeed using URLSession.dataTask](https://stackoverflow.com/a/27712427/4667835) for making URL request as OP tried, and then passing the downloaded `data` to the `UIImage(data:)` initialiser. – Dávid Pásztor Aug 16 '22 at 11:31
  • I didn't put the whole code block here, I just used an example to show that it should take the image as data and process it. When I do such an image work, I prefer caching. Let me add this process to the sample code, then is this correct? @DávidPásztor – Omer Tekbiyik Aug 16 '22 at 11:46
  • @OmerTekbiyik the problem isn't that you didn't include all code, the problem is that you included blatantly wrong code in your answer. Caching has nothing to do with using `Data(contentsOf:)`, so regardless of caching or not, that line is completely wrong. – Dávid Pásztor Aug 16 '22 at 11:48