1

I want to extract the image through the URL in the code, using URLImage. I tried the following:

import URLImage    
let url:URL = userProfileImg //<<<<< URL ErrorMessage: Cannot convert value of type 'String' to specified type 'URL'
            URLImage(url) { image in
                image
                    .resizable()
                    .aspectRatio(contentMode: .fit)
            }

I obtained the URL like this:

self.showMainView = true
            UserApi.shared.me { User, Error in
                if let name = User?.kakaoAccount?.profile?.nickname {
                    userName = name
                }
                if let profile = User?.kakaoAccount?.profile?.profileImageUrl {
                    userProfileImg = profile.absoluteString
                }
            }

How can I get the image from the URL?

2 Answers 2

2

try something like this:

if let url = URL(string: userProfileImg) {
    URLImage(url) { image in
        image
            .resizable()
            .aspectRatio(contentMode: .fit)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The ErrorMessage describes, that you cannot convert your String to an URL by assigning it. You need to change your code:

let url: URL = URL(userProfileImg)

2 Comments

I tried that, but the error comes up again ErrorMessage: No exact matches in call to initializer
Good idea, but URL() from a String gives an URL? since the string might be an invalid url. The question is then how to unbox the optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.