0

我正在为一所学校开发一个应用程序。他们将照片保存在 flickr 上。他们希望在他们的应用程序中有一个照片库页面,该页面将从他们的 flickr 页面中提取照片。我想知道从 flickr 下载照片的最简单方法是什么: 1. 我不想上传任何照片。2. 我不希望用户必须登录。 3. 我无法提供个人照片的 url,因为学校可能会更频繁地更新他们的 flickr,而不是我更新应用程序的频率。我只想让应用程序提取他们相册中的所有照片。

非常感谢,卢克

4

2 回答 2

2

试试这个代码

-(void)getUserID{

    NSString *methodName = @"flickr.people.findByUsername";

    NSString *userName = @"<Your_Username>";

    NSString *urlString = [NSString stringWithFormat:@"%@?method=%@&api_key=%@",kFlickrAPIURL,methodName,kFlickrAPIKey];
urlString = [NSString stringWithFormat:@"%@&username=%@&format=%@&nojsoncallback=1", urlString,userName,kFlickrResponseFormat];

    NSURL *url = [NSURL URLWithString:urlString];

    userIDRequest = [[NSURLRequest alloc] initWithURL: url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:userIDRequest delegate:self];
    [connection release];
}

-(void)getUserPhotos:(NSString *)userID{

    NSString *methodName = @"flickr.people.getPhotos";

    NSString *urlString = [NSString stringWithFormat:@"%@?method=%@&key=%@",kFlickrAPIURL,methodName,kFlickrAPIKey];    

    urlString = [NSString stringWithFormat:@"%@&user_id=%@&format=%@&nojsoncallback=1",urlString,userID,kFlickrResponseFormat];

    NSURL *url = [NSURL URLWithString:urlString];

    imagesRequest = [[NSURLRequest alloc] initWithURL: url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:imagesRequest delegate:self];
    [connection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSError *error = nil;

    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    if (userIDRequest == connection.originalRequest)
    {
        NSString *userId = [[jsonDic objectForKey:@"user"] objectForKey:@"id"];
        if (userId != nil)
            [self getUserPhotos:userId];
    } else if (imagesRequest == connection.originalRequest) {

        NSArray *photos = [[jsonDic objectForKey:@"photos"] objectForKey:@"photo"];

        for (NSDictionary *photo in photos)
        {
            // Get title of the image
            NSString *title = [photo objectForKey:@"title"];

            // Build the URL to where the image is stored (see the Flickr API)
            // In the format http://farmX.static.flickr.com/server/id/secret
            // Notice the "_s" which requests a "small" image 75 x 75 pixels
            NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com", [photo objectForKey:@"farm"]];
            photoURLString = [NSString stringWithFormat:@"%@/%@/%@_%@_s.jpg", photoURLString, [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];

        }
     }

  }
于 2014-02-04T11:57:23.680 回答
0

下载ObjectiveFlickr,示例文件夹中有一个示例“RandomPublicPhoto”

于 2012-08-12T16:11:38.650 回答