开发者

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

开发者 https://www.devze.com 2023-04-13 00:39 出处:网络
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 jus

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;
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号