iOS应用程序下载flickr照片

时间:2012-08-12 15:55:32

标签: iphone objective-c ios flickr

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

非常感谢, 路加

2 个答案:

答案 0 :(得分: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"]];

        }
     }

  }

答案 1 :(得分:0)

下载ObjectiveFlickr,示例文件夹

中有一个示例“RandomPublicPhoto”