从FBProfilePictureView swift获取图像

时间:2015-03-11 03:14:26

标签: ios facebook swift

使用fb登录时,以下UIView会显示用户个人资料图片

@IBOutlet var profilePictureView : FBProfilePictureView

如何获取图像以便我可以在其他地方使用/将其保存到服务器?

以下是关于FBProfilePictureView的fb文档(在Objective-C中)https://developers.facebook.com/docs/reference/ios/current/class/FBProfilePictureView 这是fb登录的快速端口,效果很好。 https://www.snip2code.com/Snippet/68653/This-is-a-swift-port-of-the-official-Fac 这是相同的代码,但在目标C中 How can I convert FBProfilePictureView to an UIImage?

2 个答案:

答案 0 :(得分:1)

正如您链接到(https://stackoverflow.com/a/12479572/2274694)的答案中所述,您可以像处理任何FBProfilePictureView一样处理UIView并遍历子视图以查找UIImageView并访问图像:

var image:UIImage!

for obj in profilePictureView.subviews {
    if obj is UIImageView {
        let objImg = obj as UIImageView
        image = objImg.image
        break
    }
}

答案 1 :(得分:1)

你也可以使用备用的想法,

<强>目标C

 NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser objectID]];

也改变类型

  • 类型:小,普通,大,方

<强>夫特

 let avatar = "https://graph.facebook.com/\(user.objectID)/picture?width=640&height=640"

或其他选择

let url = NSURL.URLWithString("https://graph.facebook.com/\(user.objectID)/picture?width=640&height=640");
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
yourimagename.image = UIImage(data: data!)