1

First off, I'm completely new to Objective-C. In my app I want to create a list of buttons with the background image set to the profile picture of my friends on facebook. To this end I was intending to use the url to the profile picture as the background image of the button. Is this even possible or would I have to download the image content first, saving it locally on the device and then use it?

What would be the most straightforward way of accomplishing this?

Thanks in advance!

T

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Tomás
  • 535
  • 6
  • 23

2 Answers2

3

As Joel said, you can use FBProfilePictureView to get the image. If you don't want to use the Facebook API, you can use this really (too) simple method, just to understand the mechanism :

// Creation of an UIImageView (which will be used as the button's background)
UIImageView *imageView = [[UIImageView alloc] init];

// Fetch the image data from a specific URL
NSString *imageURL = @"yourProfilePictureURL";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];

// Assign the image data we fetched to the UIImageView
imageView.image = [UIImage imageWithData:imageData];

// Creation of the UIButton (if you didn't do this in your story board)
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

// Set the background image of the button with the image you fetched before
[button setBackgroundImage:imageView.image forState:UIControlStateNormal];

// Add the button to the view (useless if you used storyboard)
[self.view addSubview:button];

As I said, this method is pretty straightforward, understandable and easy to use, but I suggest you to use the Facebook API instead (It will require much more work). See the FBProfilePictureViewdocumentation, then here and here for already asked questions about this class.

Community
  • 1
  • 1
Arnaud Nelissen
  • 1,082
  • 11
  • 18
  • The problem with this approach is that `dataWithContentsOfURL` is synchronous and will block the main thread (no es bueno), so you'd need to run it on a background thread. Which is probably beyond the capabilities of the OP. – Joel Oct 13 '13 at 21:12
  • If you want to avoid the Facebook SDK and just load it from a URL I'd probably use UIImageView+AFNetworking category extension so you don't have to worry about writing the async image loading from scratch. – Joel Oct 13 '13 at 21:14
  • 1
    Yep, I fully understand that, I gave this solution as a simple and straightforward one. Using UIImageView+AFNetworking category is a great idea, I agree! – Arnaud Nelissen Oct 13 '13 at 21:16
  • 1
    Guys, thank you both very much for your answers. I was torn between who to award the "answer" to seeing as Joel first responded with the suggestion of FBProfilePictureView which is perhaps the best way to go about what I need to do. But then Arnaud responded with exactly what I asked i.e. literally creating a UIIMage object and setting it as the background image of a button (and a very comprehensive example code snipper). HOwever I awarded answer to Joel just because that is the solution I will likely implement. Thanks guys. – Tomás Oct 14 '13 at 08:25
1

You can use the FBProfilePictureView from the Facebook iOS SDK to display the profile picture. Documentation available here. I'm sure if you search for FBProfilePictureView you can also find sample code.

Joel
  • 15,654
  • 5
  • 37
  • 60