2011-05-10 61 views
0

我的應用需要從Facebook獲取個人資料照片(我已經完成)。但我堅持如何使用它。我得到的圖片是.jpg格式。我想將它分配給UIImage對象並動態顯示它。從Facebook獲取圖片並在iPhone上顯示

我應該這樣做嗎?任何簡單的方法? 謝謝

+0

你說你已經檢索了圖像。你是如何檢索它的?你如何存儲它? – kball 2011-05-10 06:07:17

回答

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

看看UIImage類的參考。 有一個類的功能+(UIImage *)imageNamed:(NSString *)名稱

您可以使用UIImage * profilePicImage = [UIImage imageNamed:@「yourimage.jpg」];

相關問題