Getting Image from Facebook and display on Iphone
My app needs to get profile pic from Facebook (which I开发者_JS百科 have already done). But I am stuck at how to use it. The picture I get is in .jpg format. I want to assign it to an UIImage object and display it dynamically.
HOw should I do it? Any easy way? Thanks
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
Look at the UIImage class reference. There is a class function + (UIImage *)imageNamed:(NSString *)name
You can use UIImage *profilePicImage = [UIImage imageNamed:@"yourimage.jpg"];
This assumes that you have the data contained in an NSData object I will call pictureData
.
UIImage* profilePicture = [UIImage imageWithData:pictureData];
profilePictureView.image = profilePicture;
I prefer more deterministic memory allocation, however:
UIImage* profilePicture = [[UIImage alloc] initWithData:pictureData];
profilePictureView.image = profilePicture;
[profilePicture release];
If you have the picture on disk, one cool trick is to memory-map it to avoid memory allocation:
NSData* picData = [NSData alloc] initWithContentsOfMappedFile:@"profile.jpg"];
UIImage* profilePic = [[UIImage alloc initWithData:picData];
[picData release];
profilePicView.image = profilePic;
[profilePic release];
精彩评论