iosjsonobjective-ckey-valueflickr

Key Value pair Objective-C


I've been given a task of using a Flickr API created by a lecturer, and we have to use it to populate a tableview of a particular user. I'm able to count the number of elements etc, but I can't figure out for the life of me how to actually call the image/photo element of the pair?

This is the code:

- (NSArray *) photosForUser: (NSString *) friendUserName
{
    NSString *request = [NSString stringWithFormat: @"https://api.flickr.com/services/rest/?method=flickr.people.findByUsername&username=%@", friendUserName];
    NSDictionary *result = [self fetch: request];
    NSString *nsid = [result valueForKeyPath: @"user.nsid"];

    request = [NSString stringWithFormat: @"https://api.flickr.com/services/rest/?method=flickr.photos.search&per_page=%ld&has_geo=1&user_id=%@&extras=original_format,tags,description,geo,date_upload,owner_name,place_url", (long) self.maximumResults, nsid];

    result = [self fetch: request];

    return [result valueForKeyPath: @"photos.photo"];
}

What is used to fetch the data:

- (NSDictionary *) fetch: (NSString *) request
{
        self.apiKey = @"26225f243655b6eeec8c15d736b58b9a";

        
        NSLog(@"self.APIKey = %@", self.apiKey);

    NSString *query = [[NSString stringWithFormat: @"%@&api_key=%@&format=json&nojsoncallback=1", request, self.apiKey]
                       stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSURL *queryURL = [NSURL URLWithString: query];
    NSData *responseData = [NSData dataWithContentsOfURL: queryURL];
    if (!responseData)
        return nil;

    NSError *error = nil;
    NSDictionary *jsonContent = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableContainers error: &error];

    if (!jsonContent)
        NSLog(@"Could not fetch '%@': %@", request, error);

    return jsonContent;
}

Can anyone give me any pointers on how I can actually call the image?

Much appreciated.

edit: this is an NSLog output of what's in the JSON array received from the flickr API.

latestPhotos (
    {
    accuracy = 16;
    context = 0;
    dateupload = 1397679575;
    description =         {
        "_content" = "<a href=\"https://www.flickr.com/photos/tanjabarnes/\">
    };
    farm = 3;
    "geo_is_contact" = 0;
    "geo_is_family" = 0;
    "geo_is_friend" = 0;
    "geo_is_public" = 1;
    id = 13902059464;
    isfamily = 0;
    isfriend = 0;
    ispublic = 1;
    latitude = "34.062214";
    longitude = "-118.35862";
    owner = "66956608@N06";
    ownername = Flickr;
    "place_id" = "I78_uSpTWrhPjaINgQ";
    secret = cc17afe1b3;
    server = 2928;
    tags = "panorama losangeles beverlyhills tanjabarnes";
    title = blahlbah
    woeid = 28288701;
}
)

Solution

  • You need to construct the URL of the image using the ids in your JSON array. Like this:

    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
    

    or

    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
    

    or

    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
    

    So in your example:

    http://farm3.staticflickr.com/2928/13902059464_cc17afe1b3.jpg

    Here's how you get to your images:

      NSArray *photos = [self photosForUser:friendUserName];
      for(NSDictionary *dictionary in photos) {
        NSString *farmId = [dictionary objectForKey:@"farm"];
        NSString *serverId = [dictionary objectForKey:@"server"];
        NSString *photoId = [dictionary objectForKey:@"id"];
        NSString *secret = [dictionary objectForKey:@"secret"];
    
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://farm%@.staticflickr.com/%@/%@_%@.jpg", farmId, serverId, photoId, secret]];
    
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        // Do something with your image here
      }
    

    Reference: https://www.flickr.com/services/api/misc.urls.html