swiftswiftuiimageurl

Extracting the image from an URL


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?


Solution

  • 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)