Wednesday, 4 September 2019

Accessing modifiers of Image in custom View in SwiftUI

Currently, i have made a custom view in SwiftUI containing an Image with some details. How can we add specific image modifiers outside my view instance?

import Foundation
import SwiftUI
import Combine

struct RemoteImage: View {

    // Hold reference to our remote resource through binding
    @ObjectBinding
    private var resource: RemoteResource

    // Initialize the Image with a string
    init(urlString: String) {

        // Create our resource and request our data
        // Will fetch the resource from the internet
        self.resource = RemoteResource(urlString)
    }

    // Computed var that will return a placeholder image our our actual resource
    private var image: UIImage {
        self.resource.data.flatMap(UIImage.init) ?? UIImage(named: "placeholder")!
    }

    var body: some View {
        Image(uiImage: image)
    }
}

How would i add modifiers from an instance of RemoteImage to Image

RemoteImage(urlString: "image-url-here")
    .resizable()
    .scaledToFit()

If anyone would know a solution to my problem please let me know.



from Accessing modifiers of Image in custom View in SwiftUI

No comments:

Post a Comment