SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.1k stars 216 forks source link

SDWebImageSwiftUI doesnt display svg image #129

Open mrtrinh5293 opened 3 years ago

mrtrinh5293 commented 3 years ago

Hi, I currently working with this Api: https://restcountries.eu/rest/v2/all which has the flag image link : "flag": "https://restcountries.eu/data/afg.svg",

However, as the title already said, I could not make it to show. I've tried with different image link like https://avatars0.githubusercontent.com/u/1?v=4 and it works normally. But "flag": "https://restcountries.eu/data/afg.svg", wont work on any project Is there any suggestion? Thank you!

dreampiggy commented 3 years ago

Sounds like Apple's CoreSVG compatible issues :( See more here: https://github.com/SDWebImage/SDWebImageSVGCoder#compatibility-for-coresvg-framework

Can you have a try with iOS 14 ?

dreampiggy commented 3 years ago

Or, I suggest to create a custom UIVIewRepresentable to use SVGKImageView with SDWebImageSVGKItPlugin and SVGKit. The AnimatedImage is backed by UIImageView so it only supports Apple's own vector image solution of SVG.

https://github.com/SDWebImage/SDWebImageSVGKitPlugin. It use SVGKit, though that project sounds not maintained as well.

mrtrinh5293 commented 3 years ago

Thank you for your helps @dreampiggy ! IOS 14 is not working for AnimatedImage(url: URL(string: country.flag)!) And, This error happened when I try to add SDWebImageSVGKitPlugin dependecy : https://github.com/SDWebImage/SDWebImageSVGKitPlugin.git has no Package.swift manifest for version 1.2.0 But I'm going to try pod later on

dreampiggy commented 3 years ago

IOS 14 is not working for
 AnimatedImage(url: URL(string: country.flag)!)

Why cause this ? I see the example works well.

Check your SDWebImage version, should use the latest one, maybe 5.8.4

mrtrinh5293 commented 3 years ago

I honestly dont know what caused the problem. The only warning I have is " "The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99." Even I already checked all targets and project, they are all set to 14.0

Also, this is my very first time working with SVG and 1 month into swiftUI. I'm not familiar with a lot of stuff :(

This is my current project, in case you really want to see what is going on https://github.com/mrtrinh5293/WorldCountries

netgfx commented 3 years ago

Is it possible to display svg stored in a String? The only examples I saw were with URL.

dreampiggy commented 3 years ago

Use data:// URL format. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

dreampiggy commented 3 years ago

I think this issue is out of context. I'll close this one.

For any new question, please open new issues.

@netgfx For example, if you find the Data URL does not work, open new issues about it.

anoop4real commented 3 years ago

@mrtrinh5293 Did you initialize the SDImageSVGCoder I am also using the countries flags it worked for me. Try this

import SwiftUI
import SDWebImage
import SDWebImageSVGCoder
@main
struct MyApp: App {
    let persistenceController = PersistenceController.shared

    init() {
        setUpDependencies()
    }
    var body: some Scene {
        WindowGroup {
            CountryListView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        }
    }
}

private extension MyApp {

    func setUpDependencies() {
        SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
    }
}

And whereever you want the flag to appear use this

                            WebImage(url: URL(string: "https://restcountries.eu/data/aus.svg"),
                                     context: [.imageThumbnailPixelSize : CGSize.zero])
                                .resizable()
                                .frame(width: 50, height: 50)
                                .background(Color.gray)

This worked for me in iOS14

Fateh-Alam commented 3 years ago

@anoop4real I tried your method but it says

"Cannot find 'PersistenceController' in scope"

anoop4real commented 3 years ago

@Fateh-Alam PersistenceController will be present only if you are using a CoreData template. It has nothing to do with SDWebImage. If PersistenceController.swift is available and still you are getting this error, just see if the file is added to target else you can just delete that line