当前,我已经在SwiftUI中创建了一个自定义视图,其中包含带有一些详细信息的Image。我们如何在视图实例之外添加特定的图像修改器?
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)
}
}
如何将RemoteImage
的实例中的修饰符添加到Image
RemoteImage(urlString: "image-url-here")
.resizable()
.scaledToFit()
如果有人会想解决我的问题,请告诉我。
答案 0 :(得分:2)
如果您这样声明RemoteImage:
import Foundation
import SwiftUI
import Combine
struct RemoteImage: View {
// Hold reference to our remote resource through binding
@ObservedObject
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: Image {
Image(uiImage: image)
}
}
然后您可以像这样调用所有特定于图像的修饰符:
RemoteImage(urlString: "image-url-here").body
.resizable()
.scaledToFit()
不太理想,但是至少您不必手动重新声明每个修饰符。
答案 1 :(得分:0)
下面的代码添加了两个属性,您将可以调用
RemoteImage(urlString: myLink).resizable().renderingMode(myRenderingMode)
您可以添加所需的任何数量。
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")!
}
// Put whatever new properties you want here *****************
private var _resizable: (capInsets: EdgeInsets, resizingMode: Image.ResizingMode) = (EdgeInsets(), .stretch)
private var _renderingMode: Image.TemplateRenderingMode? = nil
// *****************
var body: some View {
Image(uiImage: image)
.resizable(capInsets: _resizable.capInsets, resizingMode: _resizable.resizingMode)
.renderingMode(_renderingMode)
}
// Put whatever new functions you want here *****************
func resizable(capInsets: EdgeInsets = EdgeInsets(), resizingMode: Image.ResizingMode = .stretch) -> RemoteImageComponent {
var rm = self
rm._resizable = (capInsets, resizingMode)
return rm
}
func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> RemoteImageComponent {
var rm = self
rm._renderingMode = renderingMode
return rm
}
}
如果您需要知道要添加到新属性中的参数,则只需单击CMD,然后单击系统功能以查看需要什么。