0

I'm trying to create a SKSpriteNode with the texture of a profile picture from the facebook users friend. I have the right ID but its still returning nil.

My code:

let pictureData = NSData(contentsOfURL: NSURL(fileURLWithPath: "http://graph.facebook.com/\(friendObject["id"]!)/picture?type=large"))
picture.texture = SKTexture(image: UIImage(data: pictureData!)!)

This is the error I'm recieving:

fatal error: unexpectedly found nil while unwrapping an Optional value

Benja0906
  • 1,437
  • 2
  • 15
  • 25

2 Answers2

0

Have you tried running that URL in a browser to confirm the ID is not nil and the URL works?

I think I also remember having this issue when I was using FBSDK. The function trying to use that image is being called before the URL has been downloaded. You need to put the code in a URL session like this.

func getFBProfileImage(profileId: String, completion: ((image: UIImage?) -> Void)) {
let url:String = "http://graph.facebook.com/\(profileId)/picture?type=large"

let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
    completion(image: UIImage(data: data))
}

   task.resume()
 }

 getFBProfileImage(friendObject(id)) { image     in
   dispatch_async(dispatch_get_main_queue()) {
        picture.texture = SKTexture(image:UIImage(data: image))
      }
 }
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • It returns this error: Use of unresolved identifier 'completion'. Yes I have tried running the URL in a browser, and it definitely works. – Benja0906 Aug 07 '16 at 08:45
  • Sorry, was trying from memory. I've edited now to include everything. You call it with getFBProfileImage() – TheValyreanGroup Aug 07 '16 at 12:30
0

I figured out the answar by seing this post:

Transport security has blocked a cleartext HTTP

All i had to do was allowing my app to connect to the website.

Community
  • 1
  • 1
Benja0906
  • 1,437
  • 2
  • 15
  • 25