Learnin
Learnin

Reputation: 1251

Get an image from FBProfilePictureView swift

When logging in with fb, the following UIView displays the users profile picture

@IBOutlet var profilePictureView : FBProfilePictureView

How would I get the image so that I can use it elsewhere/save it to a server?

Here is the fb documentation on FBProfilePictureView (in Objective-C) https://developers.facebook.com/docs/reference/ios/current/class/FBProfilePictureView Here is a swift port of the fb login, which works well. https://www.snip2code.com/Snippet/68653/This-is-a-swift-port-of-the-official-Fac And here is the same code, but in objective C How can I convert FBProfilePictureView to an UIImage?

Upvotes: 0

Views: 1186

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you can use alternate Idea also,

Objective-C

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

change the types also

  • type: small, normal, large, square

Swift

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

or another choice

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!)

Upvotes: 1

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

As explained in the answer you linked to (https://stackoverflow.com/a/12479572/2274694), you can treat the FBProfilePictureView as you would any UIView and traverse the subviews in order to find the UIImageView and access the image:

var image:UIImage!

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

Upvotes: 1

Related Questions