Use Facebook API with Objective C to find random Facebook user image

自闭症网瘾萝莉.ら 提交于 2019-12-02 19:41:08

问题


I am building an app that returns a random Facebook profile picture. So far I have the code below generating a random profile ID which sometimes does return a real profile but sometimes doesnt and just shows the generic blue Facebook face. When I use the given number on the actual website graph API it just returns false. My question is how would I put the code in so that if the random number generated returns a false profile, it just keeps generating a new random number until a real profile is returned, thus a real picture? Thanks in advance

@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:


It sounds like your problem is that you can't tell if it's a real image or not because Facebook always returns a default image?

Instead of just requesting the picture?type=large directly (which always returns an image), first make a request to the http://graph.facebook.com/USERID and read that response - which will be false or something if it's not a real user.

Loop through until you find a real user, then request the image URL.

I'm not sure why you would want to do this though... but good luck.



来源:https://stackoverflow.com/questions/7758090/use-facebook-api-with-objective-c-to-find-random-facebook-user-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!