Reputation: 115
I trying to get image in bytes and create UIImage
from it, but I can't understand how can I parse it.
My code:
func getAvatar() {
NetworkManager.shared.getAvatar(methodPath: "www.xxxxxx.xxx", params: nil) { (data) in
guard let dataValue = data as? Data else {return}
DispatchQueue.main.async {
do {
let response = try JSONSerialization.jsonObject(with: dataValue, options: .mutableContainers)
print(response)
} catch {
print("Class - HomePageHelper, Method - getAvatar = \(error)")
}
}
}
}
In postman, I get something like this:
What I am doing wrong?
Upvotes: 1
Views: 469
Reputation: 100503
You can use SDWebImage
imageView.sd_setImage(with: URL(string:urlStr), placeholderImage: UIImage(named: "placeholder.png"))
Upvotes: 0
Reputation: 360
I want to provide detailed solution.
I have used this method for download data objects from the internet.
public func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}
And here implementation of this code for download an image from the internet.
if let urlPath = model?.iconUrl, let url = URL(string: urlPath) {
NetworkClass.sharedInstance.getData(from: url) { (data, response, error) in
guard let data = data, error == nil else { return }
DispatchQueue.main.async() {
self.imageView.image = UIImage(data: data)
}
}
}
Upvotes: 0
Reputation: 5088
This response is formatted as Data
.
You can simply create instance from UIImage
using it, after your optional
chaining guard let dataValue = data as? Data else {return}
Use this dataValue
to make a UIImage
like this.
let img = UIImage(data: dataValue)
Upvotes: 3