UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Getting Remote Image: Possible to Concatenate URL?

Forums > SwiftUI

My question relates to using Paul's RemoteImage view, an example of which is in this solved post: https://www.hackingwithswift.com/forums/swiftui/loading-images/3292/6902

Specifically, I'm trying to constuct the image url on the fly, by concatenating some constants and each image's base name, to match the remote images' long absolute path. But with the code below, I get this error:

nw_protocol_get_quic_image_block_invoke dlopen libquic failed

The key lines in my code are in prefixed with an asterisk just to highlight them here:

struct JSONTest: View {
    @StateObject var data = GetJSON()

    var body: some View {

*             let thumbnailPrefix = "https://my-subhost-at.amazonaws.com/"
*             let thumbnailInfix = "/thumbnail-"
*             let thumbnailSuffix = "-optimized.jpg"

        VStack {
            ForEach(data.images) { image in
                ScrollView {
                    HStack {
                    Text(image.title)
*                   RemoteImageView(url: (thumbnailPrefix + image.url + thumbnailInfix + image.url + thumbnailSuffix))
                    }
                }
            }
        }.onAppear {
            data.fetchJSON()
        }
    }
}

Any tips on what this newbie needs to know to make something like this work? Thanks!

2      

I imagine, given that image.url is included twice in this string, that the error is here:

thumbnailPrefix + image.url + thumbnailInfix + image.url + thumbnailSuffix

Try printing this value to see what it is and if it's correct.

It's hard to say for certain, though, without more code showing what your image data structure looks like.

2      

I know it looks strange, but that's the cumbersome image path on the server. I've printed the value to confirm that it's indeed what I need. RemoteImageView's url works fine if I use a single constant (stored above the VStack) or an actual static URL in quotes, but it doesn't seem to like either of the concatenate options that I've tried below (as I get that same error):

RemoteImageView(url: (thumbnailPrefix + image.url + thumbnailInfix + image.url + thumbnailSuffix))

RemoteImageView(url: ("(thumbnailPrefix)(track.url)(thumbnailInfix)(track.url)(thumbnailSuffix)"))

FetchJSON() gets a JSON feed of data that gets modeled by a struct that includes each image's base url path (e.g., "15490"), which is only part of the actual file's absolute path on the server, which is this:

https://my-subhost-at.amazonaws.com/15490/thumbnail-15490-optimized.jpg (just an example, not a real file).

2      

Maybe I could make a custom RemoteImageView and change it so that url string that it takes is just the image's base path and the RemoteImageView does the work of concatenating the current image path with the constants (prefix, infix, suffix)?

UPDATE 1:

I tried that idea, changing the RemoteImageView ( original version of which is here: https://www.hackingwithswift.com/forums/swiftui/loading-images/3292/6902 ) but got other errors (noted below).

Here's the change I attempted:

private class Loader: ObservableObject {
    var data = Data()
    var state = LoadState.loading

     let thumbnailPrefix = "https://my-subhost-at.amazonaws.com/" // NOTE: not actual URL
     let thumbnailInfix = "/thumbs-"
     let thumbnailSuffix = "-00001.jpg"

    init(url: String) {
        guard let parsedURL = URL(string: (thumbnailPrefix + url + thumbnailInfix + url + thumbnailSuffix)) else {
            fatalError("Invalid URL: \(url)")
        }
        ...

I also tried this:

guard let parsedURL = URL(string: String(thumbnailPrefix + url + thumbnailInfix + url + thumbnailSuffix))

In conjunction with changing my original call to RemoteImageView(url: image.url).

Here's the error:

NSURLErrorRelatedURLSessionTaskErrorKey=("LocalDataTask <F5CAD68F-C4BF-474E-84F6-C910D5D27431>.<89>"), NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <F5CAD68F-C4BF-474E-84F6-C910D5D27431>.<89>, NSUnderlyingError=0x600001ca2c10 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}*

UPDATE 2:

Ooops! My original code attempt actually worked (i.e., concatenating in the call, not customizing RemoveImageView). The problem was that I had fetched a JSON recordset from one server but was constructing the URL path that included a different subdomain on that server. Once I corrected the thumbnailPrefix constant, the remove images appeared. Uggh.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.