1

首先,我对 Objective-C 完全陌生。在我的应用程序中,我想创建一个按钮列表,其中背景图像设置为我在 Facebook 上朋友的个人资料图片。为此,我打算使用个人资料图片的 url 作为按钮的背景图片。这甚至可能吗,还是我必须先下载图像内容,将其保存在本地设备上然后使用它?

实现此目的最直接的方法是什么?

提前致谢!

4

2 回答 2

3

正如乔尔所说,您可以使用FBProfilePictureView来获取图像。如果你不想使用 Facebook API,你可以使用这个非常(太)简单的方法,只是为了理解机制:

// 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];

正如我所说,这种方法非常简单、易于理解且易于使用,但我建议您改用 Facebook API(这需要更多的工作)。请参阅文档,然后在此处此处查看有关此类的已询问问题。 FBProfilePictureView

于 2013-10-13T21:00:10.033 回答
1

您可以使用FBProfilePictureViewFacebook iOS SDK 中的 来显示个人资料图片。此处提供文档。我敢肯定,如果您搜索,FBProfilePictureView您也可以找到示例代码。

于 2013-10-13T20:48:48.057 回答