键值对Objective-C

时间:2014-05-21 12:10:50

标签: ios json api

所以我被赋予了使用由讲师创建的flickr API的任务,我们必须使用它来填充特定用户的tableview。我可以计算元素的数量等,但我无法弄清楚如何实际调用该对的图像/照片元素?

这是代码:

- (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"];
}

用于获取数据的内容:

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

任何人都可以给我任何关于如何实际调用图像的指示吗?

非常感谢。

编辑:这是从flickr API收到的JSON数组中的内容的NSLog输出。

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

1 个答案:

答案 0 :(得分:0)

您需要使用JSON数组中的ID构建图像的URL。像这样:

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

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

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

所以在你的例子中:

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

以下是您获取图片的方式:

  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
  }

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