WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

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!

   

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.

   

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

   

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.

   

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.