如何从快速关闭中返回

问题描述 投票:0回答:1

我有一个返回错误“无法将类型'DataRequest'的返回表达式转换为类型'UIImage'的函数”

我了解为什么会发生错误,但不知道如何解决。这是代码:

func getProfilePicture() -> UIImage {
    Alamofire.request(GIDSignIn.sharedInstance()?.currentUser.profile.imageURL(withDimension: 75) ?? "https://httpbin.org/image/png").responseImage { response in

        if let image = response.result.value {
            return image

        }
    }
}

我已经尝试过了,但是我不知道如何在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完成处理程序的方法,但是我不知道如何使用它来解决问题。谢谢!

swift xcode alamofire google-signin swiftui
1个回答
1
投票

使用如下的完成处理程序,

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)

            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.