从Facebook获取图像并在Iphone上显示

时间:2011-05-10 05:59:23

标签: iphone xcode

我的应用程序需要从Facebook获取个人资料照片(我已经完成了)。但我坚持如何使用它。我得到的图片是.jpg格式。我想将它分配给UIImage对象并动态显示它。

我应该这样做吗?任何简单的方法? 感谢

3 个答案:

答案 0 :(得分:3)

NSURL *url = [NSURL URLWithString: @"http://facebook.jpg"]; // facebook.jpg is the url of profile pic
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]]; //  or something similar

答案 1 :(得分:0)

这假设您拥有NSData对象中包含的数据,我将调用pictureData

UIImage* profilePicture = [UIImage imageWithData:pictureData];
profilePictureView.image = profilePicture;

我更喜欢更确定的内存分配:

UIImage* profilePicture = [[UIImage alloc] initWithData:pictureData];
profilePictureView.image = profilePicture;
[profilePicture release];

如果你有磁盘上的图片,一个很酷的技巧是内存映射它以避免内存分配:

NSData* picData = [NSData alloc] initWithContentsOfMappedFile:@"profile.jpg"];
UIImage* profilePic = [[UIImage alloc initWithData:picData];
[picData release];
profilePicView.image = profilePic;
[profilePic release];

答案 2 :(得分:0)

查看UIImage类引用。 有一个类函数+(UIImage *)imageNamed:(NSString *)name

你可以使用UIImage * profilePicImage = [UIImage imageNamed:@“yourimage.jpg”];