gpt4 book ai didi

swift - 使用 Facebook 个人资料图片作为个人资料图片 Swift

转载 作者:行者123 更新时间:2023-11-28 15:40:41 25 4
gpt4 key购买 nike

我正在获取 facebook 的个人资料图片并将其显示为我的应用程序中的个人资料图片。这是代码。

if let user = FIRAuth.auth()?.currentUser{
let photoUrl = user.photoURL
let name = user.displayName

self.FacebookUser.text = name

let storage = FIRStorage.storage()

//refer your particular storage service
let storageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com")
let profilePicRef = storageRef.child(user.uid+"/profile_pic.jpg")

profilePicRef.data(withMaxSize: 1 * 1024 * 1024, completion: { (data, error) -> Void in
if (error == nil){

self.FacebookPic.image = UIImage(data: data!)

}else{

print("Error downloading image:" )


}
})

if(self.FacebookPic.image == nil)
{
var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height": 300, "width": 300, "redirect": false], httpMethod: "GET")
profilePic?.start(completionHandler: {(_ connection, result, error) -> Void in
// Handle the result


if error == nil {
if let dictionary = result as? [String: Any],
let data = dictionary["data"] as? [String:Any],
let urlPic = data["url"] as? String{

if let imageData = NSData(contentsOf: NSURL(string: urlPic)!as URL){

let uploadTask = profilePicRef.put(imageData as Data, metadata: nil) {
metadata, error in

if (error == nil)
{
let downloadurl = metadata!.downloadURL
}
else
{
print("Error in downloading image")
}
}

self.FacebookPic.image = UIImage(data: imageData as Data)
}}}})}
}else{
}

//The END of the Facebook user and picture code

我能够让它工作几天,但现在它不再工作了,我已经逐行检查了它,老实说,我无法弄清楚为什么它不工作。

最佳答案

我使用了这段代码:

func pictureFromFirebase(loginMethod: Int)
{
if loginMethod == 0 //FB
{
var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height":300, "width":300, "redirect":false], httpMethod: "GET")
let profilePicRef = storageRef.child((user?.uid)!+"/profile_pic.jpg")
profilePicRef.data(withMaxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
// Uh-oh, an error occurred!
// but we don't need to do anything yet. Try to download the profile pic
}
if (data != nil)
{
print("no need to download image from facebook")
self.profileImage.image = UIImage (data: data!)
}
else
{
// THIS IS THE BLOCK THAT HAS BEEN MOVED
// WHICH WILL NOW BE EXECUTED IN TWO CONDITIONS -
// 1. AN ERROR IN THE DOWNLOAD
// 2. NO PROFILE PIC AVAILABLE
print("downloading image from facebook")
profilePic?.start(completionHandler: {(_ connection, _ result, _ error) -> Void in
if (error == nil)
{
if let dictionary = result as? [String:Any], let data = dictionary["data"] as? [String:Any],
let urlPic = data["url"] as? String {
if let imageData = NSData(contentsOf: NSURL(string: urlPic)! as URL)
{
let uploadTask = profilePicRef.put(imageData as Data, metadata: nil){
metadata, error in
if (error == nil)
{
let downloadUrl = metadata!.downloadURL
}
else
{
print("error in downloading image")
}
}
self.profileImage.image = UIImage(data: imageData as Data)
}
}

}
})
}

}
}
}

来自这篇文章Second If statement gets called before first statement finished in one function并且有效

关于swift - 使用 Facebook 个人资料图片作为个人资料图片 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43739176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com