UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Load pictures from a JSON

Forums > 100 Days of Swift

Hi guys,

I'm reviewing the 100 days of hacking with swift and finished the Milestone: Projects 13-15 challenge. I'd like to improve the app and to display an image of the country flag in my cell.imageView. So I was going for a solution like this:

let country = countries[indexPath.row]

if let imageURL = URL(string: country.flag) {
           print(imageURL)
           if let data = try? Data(contentsOf: imageURL) {
               print(data)
               cell.imageView?.image = UIImage(data: data)
           }
       }

The print output for imageURL would be this: https://restcountries.eu/data/afg.svg 47079 bytes

Any hints why isn't that working?

Thanks for helping me out.

https://www.hackingwithswift.com/guide/6/3/challenge

3      

You can't create a UIImage from SVG data natively in Swift. You would need to use a third-party library of some sort.

4      

Thanks @roosterboy. That's something to solve on this - stay at home - weekend. ;-)

3      

@fxdlsRider I have the same problem and hard time to figure the svg thingy out. Did you make it to work? I normally using SDWebImageSwiftUI for other type of image but SVG is too much work :(

3      

Hello. I noticed @roosterboy mentioned that we can't create a UIImage from SVG data natively in Swift. Is this still the case for iOS 13 with Xcode 12? Because I'm trying to do that but it's not working. I'm not sure if I'm doing something incorrect but here's my code:

guard let url: URL = URL(string: "https://somewebsite.com/my_svg.svg") else {
    fatalError("Failed to load url")
}

DispatchQueue.global().async { [weak self] in
    if let data = try? Data(contentsOf: url) {
        if let image = UIImage(data: data) {
            DispatchQueue.main.async {
                self?.svgImageView.contentMode = .scaleAspectFit
                self?.svgImageView.image = image
                self?.view.layoutSubviews()
            }
        }
        else {
            print("Invalid image")
        }
    }
}

3      

Well, since you cannot create an SVG image from data in Swift, if it's not working for you then that is correct behavior.

3      

@roosterboy Gotcha, thanks for confirming!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.