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?

Christophe
  • 68,716
  • 7
  • 72
  • 138
DEUK_YEONG
  • 51
  • 4

2 Answers2

2

try something like this:

if let url = URL(string: userProfileImg) {
    URLImage(url) { image in
        image
            .resizable()
            .aspectRatio(contentMode: .fit)
    }
}
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)
Gebes
  • 312
  • 2
  • 9
  • I tried that, but the error comes up again ErrorMessage: No exact matches in call to initializer – DEUK_YEONG Jun 18 '22 at 07:38
  • Good idea, but [`URL()`](https://developer.apple.com/documentation/foundation/url) from a `String` gives an `URL?` since the string might be an invalid url. The question is then how to unbox the optional. – Christophe Jun 18 '22 at 08:37