我已经尝试过这个,但我不知道如何在 SwiftUI 视图中使用结果:
func getProfilePicture(_ completion: @escaping ((UIImage) -> Void)) {
Alamofire.request(GIDSignIn.sharedInstance()?.currentUser.profile.imageURL(withDimension: 75) ?? "https://httpbin.org/image/png").responseImage { response in
if let image = response.result.value {
completion(image)
}
}
}
如果您可以提供帮助,我想将从完成处理程序返回的图像放入此视图中:
struct ProfileView: View {
let profileInfo = ProfileInfo()
var placeHolderImage = Image(systemName: "person")
var body: some View {
Group {
placeHolderImage
.clipShape(Circle())
.overlay(
Circle().stroke(Color.white, lineWidth: 4))
.shadow(radius: 10)
.padding(10)
}
}
}
我希望它返回一个 UIImage,这样我最终可以在 SwiftUI 视图中使用它。我已经尝试过使用带有 @escaping 完成处理程序的方法,但我不知道如何使用它来解决问题。谢谢!
你可以尝试一下让这个:
struct ProfileView: View {
@State var placeHolderImage = Image(systemName: "person")
var body: some View {
Group {
placeHolderImage
.clipShape(Circle())
.overlay(
Circle().stroke(Color.white, lineWidth: 4))
.shadow(radius: 10)
.padding(10)
}.onAppear{
getProfilePicture{ image in
self.placeHolderImage = Image(uiImage: image)
}
}
}
}
When ProfileView
看来它会打电话getProfilePicture
. The image
指定于image in
(调用函数时)是完成处理程序传递的内容(completion(image)
)。然后你可以做的是改变你的placeHolderImage
到你得到的东西getProfilePicture
,但在你这样做之前,你需要让你的uiImage
进入一个Image
。还要确保添加@State
关键字到你的变量,所以一旦它改变你的View
已更新。
希望有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)