Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error mapping image with Moya. #2290

Open
ThaC0derDre opened this issue Nov 17, 2022 · 0 comments
Open

Error mapping image with Moya. #2290

ThaC0derDre opened this issue Nov 17, 2022 · 0 comments

Comments

@ThaC0derDre
Copy link

ThaC0derDre commented Nov 17, 2022

I'm encountering an issue when mapping an image.
My service works when calling other data. Swapping out Moya with AlamoFireImage produces the desired results.

enum AvatarService {
    case grabAvatarImage(url: String)
}

extension AvatarService: TargetType {
    var baseURL: URL {
        URL(string: "https://avatars.githubusercontent.com/u/")!
    }
    
    var path: String {
        switch self {
        case .grabAvatarImage(let url):
            let splitUrl = url.components(separatedBy: "u/")
            return splitUrl.last!
        }
    }

var method: Moya.Method { .get }
    
var task: Moya.Task { .requestPlain }

Calling from ViewModel:

func getLanguage() {
        moya.provider.request(.grabAvatarImage(url: avatarUrl)) { [weak self] result in
            switch result {
            case .failure(let error):
                print("Error fetching Image:", error)
            case .success(let response):
                guard
                    let self = self,
                    let receivedImage = try? response.mapImage()
                else {
                    print("🔥 Failed to map image")
                    return
                }
                self.Image = receivedImage()
            }
        }
    }

"🔥 Failed to map image" gets printed in console. Any help would be greatly appreciated. Thanks

Using Moya 15.0.0 via SwiftPackageManager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant