Download high resolution profile image Facebook Graph API - swift

Using Swift 3.0, I can download the current users image using this graph call function:
fileprivate func getCurrenUserHighResImageURL() -> String? {
var photoURLOutput : String?
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "picture.type(large)"])
graphRequest.start(completionHandler: { connection, result , error -> Void in
if ((error) != nil) {
print("Error: \(error)")
} else {
let castedResult = result as? [String : AnyObject]
if let castedResult = castedResult {
if let photURL = castedResult["picture"] as? [String : AnyObject] {
let photoData = photURL["data"] as? [String : AnyObject]
photoURLOutput = photoData?["url"] as? String
if let photoURLOutput = photoURLOutput {
CURRENT_USER_URL.updateChildValues(["highResImageURL" : "\(photoURLOutput)"])
}
}
}
}
})
return photoURLOutput
}
However this only returns a 200 x 200 pixel image. Is there any way to make a graph call to to a higher resolution?
Ive seen people making calls to the graph API using a URL like this: https://graph.facebook.com/userId/picture?width=640&height=640
as mentioned in this post : Facebook Graph Profile Picture Link
But when i attempt to download an image from that URL...
func loadUserImage(fromURL urlString:String?) {
if urlString != nil {
let imgURL: URL = URL(string: urlString!)!
let request: URLRequest = URLRequest(url: imgURL)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
(data, response, error) -> Void in
if (error == nil && data != nil) {
func display_image() {
let userImage = UIImage(data: data!)
self.userImage.image = userImage
}
DispatchQueue.main.async(execute: display_image)
}
})
task.resume()
}
}
there is no image to be found. (I know this function works because it works on all other images). When I type the link into my browser directly it get a JSON error back saying i don't have authorization.
Has anyone had any luck make a graph call like this? Is there some syntax I have overlooked which will return a higher res profile image?

This is what I have to request 200x200 on an iPhone 7 and 300x300 on a plus size. However I don't think you get an image back with those exact sizes. It might be slightly larger.
let deviceScale = Int(UIScreen.main.scale)
let width = 100 * deviceScale
let height = 100 * deviceScale
let parameters = ["fields": "first_name, last_name, picture.width(\(width)).height(\(height))"]
In summary, the syntax for the param to request a 400x400 would be picture.width(400).height(400).

Related

Facebook Graph API: Swift SDK not working: GraphRequest

I've been trying to follow Facebooks documentation exactly but can't understand why my code doesn't work, even though it is exactly as in the docs. I am already signing into Facebook on my app, but I need to get the profile data.
Here is my function.
func fetchFacebookUser() {
let connection = GraphRequestConnection()
connection.add(GraphRequest(graphPath: "/me")) { httpResponse, result in
switch result {
case .success(let response):
print("Graph Request Succeeded: \(response)")
case .failed(let error):
print("Graph Request Failed: \(error)")
}
}
connection.start()
}
You'll notice it is exactly like the documentation:
https://developers.facebook.com/docs/swift/graph
But I can't even build the project because I get this error in Xcode:
Contextual closure type '(GraphRequestConnection?, Any?, Error?) -> Void' expects 3 arguments, but 2 were used in closure body
Insert ',<#arg#> '
It has the 'Fix' button but that just breaks the whole thing and Xcode (red warning) complains that none of the cases in the switch statement exist.
I have searched everywhere, the closest solution I got was this SO post: Facebook GraphRequest for Swift 5 and Facebook SDK 5
But that had no answers :(
I'm sure someone has called the Graph API using Swift? What am I missing here?
However i implement facebook login my app which works fine.
On button click I called facebook login using LoginManager.
#IBAction func continueWithFacebook(_ sender: Any) {
let fbLoginManager : LoginManager = LoginManager()
fbLoginManager.logIn(permissions: ["email"], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : LoginManagerLoginResult = result!
if (result?.isCancelled)!{
return
}
if(fbloginresult.grantedPermissions.contains("email")) {
self.getFBUserData()
}
}
}
}
After that I get the data of user using GraphAP
func getFBUserData() {
if((AccessToken.current) != nil){
GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
guard let userDict = result as? [String:Any] else {
return
}
if let picture = userDict["picture"] as? [String:Any] ,
let imgData = picture["data"] as? [String:Any] ,
let imgUrl = imgData["url"] as? String {
self.uURLString = imgUrl
/*let url = URL(string: imgUrl)*/
print(imgData)
}
if let n = userDict["name"] as? String {
self.name = n
}
if let e = userDict["email"] as? String {
self.email = e
}
self.socialLogIn()
}
})
}
}
Add in your pod file -
pod 'FBSDKCoreKit' and
pod 'FBSDKLoginKit'
Try using like this you will get your derided result.
Happy Coding

Use Facebook profile picture as you profile picture Swift

I am getting facebook's profile picture and displaying it as the profile picture in my app. Here is the code.
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
I was able to get it working for a couple days and now it doesn't work anymore, I have gone through it line by line and I honestly can't figure out why it is not working.
I used this 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)
}
}
}
})
}
}
}
}
from this post Second If statement gets called before first statement finished in one function and it worked
you just get your facebook profile pic. using this url and put the url in your UIImageview
let profilepicURl = "https://graph.facebook.com/\(user_id_fb)/picture?type=large" //user_id_fb like 1251246454544 your facebook ID

Get Facebook profile picture from URL

I want to upload the profile picture from Facebook to Firebase. I tried this answer: Upload Facebook image URL to Firebase Storage
However, Swift is giving me errors on the third line of code of that answer. The code is:
let dictionary = result as? NSDictionary
let data = dictionary?.object(forKey: "data")
let urlPic = (data?.objectForKey("url"))! as! String
Swift is telling me: Cannot call value of non-function type 'Any?!' after I changed the code to what Swift keeps suggesting me:
let urlPic = ((data as AnyObject).object(ForKey: "url"))! as! String
What is the code to use when I want to retrieve the profile picture from Facebook? My goal is to also store it into Firebase, but that will come after I get the profile picture first.
The answer is in Swift 1.2
I took reference here and implemented also
You can do this:
// accessToken is your Facebook id
func returnUserProfileImage(accessToken: NSString)
{
var userID = accessToken as NSString
var facebookProfileUrl = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")
if let data = NSData(contentsOfURL: facebookProfileUrl!) {
imageProfile.image = UIImage(data: data)
}
}
This is the way I got Facebook id:
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
if let id: NSString = result.valueForKey("id") as? NSString {
println("ID is: \(id)")
self.returnUserProfileImage(id)
} else {
println("ID es null")
}
}
})
}

Swift. Facebook profile picture returning a question mark?

Okay so when I try to recieve the users profil picture, the picture returns a white box with a question mark in it?
Heres my code:
func getProfilePic(fid: String) -> SKTexture? {
let imgURL = NSURL(string: "http://graph.facebook.com/" + fid + "/picture?type=large")
let imageData = NSData(contentsOfURL: imgURL!)
let imageUI = UIImage(data: imageData!)
let image = SKTexture(image: imageUI!)
return image
}
func getFBUserData() {
if((FBSDKAccessToken.currentAccessToken()) != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
print(result)
if let userData = result as? NSDictionary {
personalUserID = userData["id"] as! String
}
} else {
print("error")
}
})
}
picture.texture = getProfilePic("\(personalUserID)")
How do I get it to show the right picture?
I'm guessing your profile picture is not available for the public. Now you're not providing an access token with the request, so the request is handled as unauthorised - so you see only what the public eye sees.
To fix it:
func getProfilePic(fid: String) -> SKTexture? {
let imgURL = NSURL(string: "http://graph.facebook.com/" + fid + "/picture?type=large&access_token=" + FBSDKAccessToken.currentAccessToken().tokenString)
let imageData = NSData(contentsOfURL: imgURL!)
let imageUI = UIImage(data: imageData!)
let image = SKTexture(image: imageUI!)
return image
}
Also, you'd want to use https and the current API version v2.5 to make the requests, otherwise your code might break in any second when Facebook makes changes. So, with that in mind:
func getProfilePic(fid: String) -> SKTexture? {
let imgURL = NSURL(string: "https://graph.facebook.com/v2.5/" + fid + "/picture?type=large&access_token=" + FBSDKAccessToken.currentAccessToken().tokenString)
let imageData = NSData(contentsOfURL: imgURL!)
let imageUI = UIImage(data: imageData!)
let image = SKTexture(image: imageUI!)
return image
}
That should do the trick.

Swift and Facebook SDK 4, Graph API

Trying to put Facebook integration into a trial app I'm working on and can't seem to find an efficient way to make a friends list with a small profile pic from fb beside them. everything works flawless but the lengthy wait times on the data fetch. Please help with anything that will speed up image fetch times. Currently it takes about 10 seconds to fetch.
func facebookProfilePicRequest(){
let graphConnection = FBSDKGraphRequestConnection()
for result in self.facebookFriends {
if let resultingId = result["id"] as? String{
let profilePicRequest = FBSDKGraphRequest(graphPath: "/\(resultingId)/picture?redirect=false", parameters: nil)
graphConnection.addRequest(profilePicRequest){
(connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
if(error != nil){
if error.code == 1009{
println("No Internet Connection, \(error.code))")
}
}else{
if let data: NSDictionary = result as? NSDictionary{
if let urlDictionary: NSDictionary = data["data"] as? NSDictionary{
if let urlString: NSString = urlDictionary["url"] as? NSString{
let url: NSURL = NSURL(string: urlString as String)!
var request1: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if error == nil{
self.facebookFriendsImages.append(data)
self.tableView.reloadData()
}
})
}
}
}
}
}
}
}
graphConnection.start()
}
Instead of fetching all the images by yourself, you could use Facebook provided FBSDKProfilePictureView. You only have to set the user profile ID, and it would load the picture for you.
This should improve things as it doesn't have to wait for all the images to be loaded before showing them, and it also load them asynchronously as you scroll the tableview.