2011-10-13 57 views
0

我正在構建一個應用程序,該應用程序返回一個隨機的Facebook個人資料圖片。到目前爲止,我有下面的代碼生成一個隨機配置文件ID,有時會返回一個真實的配置文件,但有時不會,只是顯示通用的藍色臉譜。當我在實際的網站圖API上使用給定的數字時,它只是返回false。我的問題是如何將代碼放入,以便如果生成的隨機數字返回false配置文件,它會一直生成一個新的隨機數,直到返回一個真實的配置文件,從而成爲真實的圖片?在此先感謝使用Objective-C的Facebook API來查找隨機的Facebook用戶圖像

@implementation FacebookPicturesViewController 

- (IBAction) nextImagePush { 

    NSString *prefix = @"http://graph.facebook.com/"; 
    NSString *profileId = [NSString stringWithFormat:@"%09d", abs(arc4random())]; 
    NSLog(@"profileId: %@", profileId); 
    NSString *suffix = @"/picture?type=large"; 
    NSString* url= [NSString stringWithFormat:@"%@%@%@", prefix, profileId, suffix]; 
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; 
    [imageView setImage:img]; 
    imageCount++; 

    NSLog(@"profileId: %@", profileId); 

    if (imageCount >= [imageArray count]){ 
     imageCount = 0; 
    } 
} 

回答

1

這聽起來像你的問題是,你不能告訴,如果它是真實的圖像或不是因爲Facebook總是返回的默認圖像?

而不是僅僅請求picture?type=large直接(它總是返回的圖像),第一做出的http://graph.facebook.com/USERID的請求和讀取響應 - 這將是false或東西,如果它不是一個真正的用戶。

循環播放,直到找到真實用戶,,然後請求圖片URL。

我不確定你爲什麼要這樣做,但是,祝你好運。

相關問題