2012-08-12 150 views
0

我正在開發一個學校的應用程序。他們將照片保存在flickr上。他們希望在他們的應用中添加照片庫頁面,以便從他們的flickr頁面中提取照片。我想知道從flickr下載照片的最簡單方法是什麼: 1.我不想上傳任何照片。 2.我不希望用戶必須登錄。 3.我無法提供個別照片的URL,因爲學校可能會更新他們的flickr的頻率比我更新應用程序的頻率更高。我只是想讓應用將他們的相冊中的所有照片都拉出來。iOS應用程序下載flickr照片

非常感謝, 盧克

回答

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

     } 
    } 

    }