【问题标题】:Key Value pair Objective-C键值对 Objective-C
【发布时间】:2014-05-21 12:10:49
【问题描述】:

所以我得到了使用讲师创建的 flickr API 的任务,我们必须使用它来填充特定用户的表格视图。我可以计算元素的数量等,但我一生都无法弄清楚如何实际调用这对图像/照片元素?

这是代码:

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

【问题讨论】:

  • 感谢您的链接,但我认为我需要利用所提供的内容。
  • 你得到了无效的 JSON,你用错误的 API 传递了服务器,检查一下
  • 对不起,我不太清楚你的意思,你能解释一下吗?干杯
  • 你的编码很好,但是你从服务器得到了无效的响应,你想检查你的响应复制你的结果并粘贴到jsonviewer.stack.hu这里并检查

标签: ios json api


【解决方案1】:

您需要使用 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

【讨论】:

  • 太棒了,谢谢,但我仍然不确定我实际上是如何从 nsmutable 数组中“检索” id/secret/serverID 的?
  • 数组日志中的对象#0 是一个字典。先获取,再使用关键路径。
  • 那是我不确定的部分。我有组合三个 ID 的代码我只是不知道如何从数组中实际获取 ID。编辑:刚刚看到您的编辑,谢谢 mprivat。现在看看
  • 感谢您迄今为止的帮助,所以我查看了代码,它是有道理的,我已经添加了它,但每次我尝试使用它(即 NSLog)该方法,它只是不断循环和黑屏?但是当我在不同的类方法调用中执行此操作时,它只会返回 null。有什么想法吗?
  • 哇 mprivat 你是个传奇,我真的很感谢你的帮助。如果你曾经在澳大利亚,你可以无限畅饮。感谢您对一切工作方式的清晰描述。现在有道理了,我出错的地方是我一直试图专注于调用一个键,然后是一个数组上的 forKey 对象,它一直在崩溃。谢谢大家
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多